* Main.do reads in the 2019 and 2022 Raven data and undertakes estimations */

* log file
log using raven.log, replace name(raven)

* configure Stata
version 18
set processors 4
set more off
set scheme s1color
set seed 987654321
timer clear 1
timer on 1

* tell us what version ran
about

* package install
*capture: ssc install vcemway
*capture: ssc install norm
*capture: ssc install corrtable

* changing, new code, so be sure to get the latest version
*net install qmodel, replace from(http://www.imm.ki.se/biostatistics/stata)
*ssc install mylabels

* globals
global doRiskLink "y"
global doBaseline "y"
global doData "y"

global doRECOVERY "y"
global getRECOVERED "y"
global useRECOVERED "n"

global doGignac "y"
global doArthurDay "y"

* some slower graphs and estimates, might skip
global doGraphs "y"
global doEstimates "y"

* select font for graphs
graph set window fontface "Candara"
global table_font "Candara"
global text_font "Candara"

* directory for figures
capture: mkdir figures

* local for short-forms
local arthur_day "1 4 8 11 15 18 21 23 25 30 31 35"
local bors_stokes "3 10 12 15 16 18 21 22 28 30 31 34"

* local for types of rules
local dist2 "22 23 30 31 32 35 36"
local addsub "7 9 12 16 19 33"
local pairwise "3 4 5 6 10 14 25 26"
local dist3 "1 8 13 17 21 26 27 29 31 32 34"
local n_dist2 = 7
local n_addsub = 6
local n_pairwise = 8
local n_dist3 = 11

* local for core demographics
local demog "female age age2 black business noreligion working gpaHI"
local demog "female age black business noreligion working gpaHI"
local demog "female age black business noreligion working"

* drop age, little variation
*local demog "female black business noreligion working"

* extra demographics for looking at interaction of black and female
local demog_x "age business noreligion working"
local demog_x "business noreligion working"

* global for demographics for belief recovery
global demog_recovery "female age age2 black business noreligion working gpaHI"

* program to label variables
program define get_label

	capture: label variable sid "Subject ID"
	capture: label variable female "Female"
	capture: label variable age "Age in years"
	capture: label variable age17 "Age over 17"
	capture: label variable black "Black or African-American"
	capture: label variable business "Business Major"
	capture: label variable noreligion "Atheist"
	capture: label variable working "Working"

	capture: label variable incentive "Financial incentive"
	capture: label variable apm_correct "Correct answer on RAPM question"
	capture: label variable arthur_day "12-item Short Form version of RAPM due to Arthur and Day [1994]"
	capture: label variable bors_stokes "12-item Short Form version of RAPM due to Bors and Stokes [1998]"
	capture: label variable tokens "Number of tokens"
	capture: label variable time "Time allowed"
	capture: label variable incentives "Incentives for correct answers provided"
	capture: label variable increasing "Increasing financial incentives"
	capture: label variable baseline "Baseline pen-and-paper, non-salient task"
	capture: label variable time_constraint "Binding time constraint of 40 minutes"
	capture: label variable one_token "One token only, or forced to pick one answer"
	capture: label variable one_tokenPP "One token only, or forced to pick one answer, and pen and paper"
	capture: label variable one_tokenS "One token only and Scrambled order of presentation"
	capture: label variable one_tokenP "One token only and Progressive order of presentation"
	capture: label variable standard "Standard treatment with QSR, using Progressive order, constant incentives and no effective time constraint"
	capture: label variable baseline_correct "Average baseline correct answers for each question"
	capture: label variable accuracy "Share of tokens allocated to the correct answer"
	capture: label variable earnings "Earnings"
	capture: label variable earningsN "Earnings as a fraction of the maximum if 100% correct"
	capture: label variable raven_score "Aggregate Raven Score"

end

/*
* generate dummy graph titles
set obs 500
generate s = uniform()
generate t = uniform()
twoway (scatter s t), title("Figure 1: Raven-Like Problem", size(vlarge)) subtitle("Source: P.A. Carpenter, M.A. Just and P. Shell," "What One Intelligence Test Measures: A Theoretical Account of" "the Processing in the Raven Progressive Matrices Test," "{it:Psychological Review}, 97(3), 1990, 404-431" "    " "    ", size(small) margin(medsmall)) saving(figures/`appendix'figure1, replace)
twoway (scatter s t), title("Figure 3: Belief Elicitation Interface:" "Initial Display and Completely Correct Response" "    " "    ", size(vlarge)) saving(figures/`appendix'figure3, replace)
twoway (scatter s t), title("Figure 4: Belief Elicitation Interface:" "Two Possible Displays of Confidence" "    " "    ", size(vlarge)) saving(figures/`appendix'figure4, replace)

twoway (scatter s t), title("Figure 17: Example Lineups Used in" "Eyewitness Identification Experiments", size(vlarge)) subtitle("Source: Wixted, J.T. and Wells, G.L., The Relationship Between Eyewitness Confidence and" "Identification Accuracy: A New Synthesis, {it:Psychological Science in the Public Interest}, 18(1) 2017, 10�65" " " "Images: Ma, D.S., Correll, J. and Wittenbrink, B., The Chicago Face Database:" "A Free Stimulus Set of Faces and Norming Data, {it:Behavior Research Methods}, 47, 2015, 1122�1135", size(small) span margin(medsmall)) saving(figures/`appendix'figure17, replace)
*/

* read in the risk link data
if "$doRiskLink" == "y" {

/*
	raven-1 -> pen/paper baseline
	raven-2 -> pen/paper baseline 
	raven-3 -> pen/paper baseline 
	raven-4 -> 90 minutes / 80 tokens
	raven-5 -> 90 minutes / 80 tokens 
*/

	import delimited using raven_risklink.txt, varnames(1) clear
	generate int raven = 0
	replace raven = 1 if substr(session,1,5) == "raven"
	tab raven, missing
	keep if raven==1
	list, noobs

	* rename to match II data
	rename recruiterid LinkID
	sort LinkID
	merge 1:1 LinkID using LinkID_characteristics
	keep if _merge==3
	drop _merge
	tab female, missing
	tab raven_score, missing

	* get correct gpaHI interpretation
	replace gpaHI = gpaHI + gpaVHI

	rename session Session
	sort Session seat
	save tmp_risklink, replace

* end of $doRiskLink
}

* read in the baseline data
if "$doBaseline" == "y" {

	* read correct answers
	import excel using "GSU Raven Data -- Baseline 1.xlsx", sheet("Correct") firstrow clear
	des
	list
	sort question
	save tmp, replace

	* read raw data
	import excel using "GSU Raven Data -- Baseline 1.xlsx", sheet("Data") firstrow clear
	des

	* reshape
	reshape long q, i(session seat) j(question)
	rename q answer

	* merge with correct
	sort question
	merge m:1 question using tmp
	drop _merge
	erase tmp.dta

	* see if correct
	generate int apm_correct = 0
	replace apm_correct = 1 if answer == correct
	rename question apm_question
	summ apm_correct
	sort session seat
	save tmp, replace

	* link to characteristics
	tab session, missing
	generate str10 Session = "raven-" + string(session)
	compress Session
	tab Session, missing
	sort Session seat
	merge m:1 Session seat using tmp_risklink
	tab _merge
	drop if _merge==2
	drop _merge

	* save data
	generate int data2022= 0
	save raven_gsu_baseline, replace

	* save for merging with other non-salient data
	collapse (mean) apm_correct, by(apm_question)
	generate apm_gsu = 100*apm_correct
	drop apm_correct
	sort apm_question
	save gsu_baseline, replace

	* average scores
	use raven_gsu_baseline, replace
	tab apm_question apm_correct, row nofreq
	save tmp, replace
	collapse (sum) apm_correct (mean) raven_score CRT* `demog', by(session seat)
	sort session seat
	generate setI_score_pct = 100*(raven_score/12)
	generate score_pct = 100*(apm_correct/36)
	rename apm_correct score
	format score score_pct setI_score_pct %5.0f
	sort score_pct
	summ score_pct, detail
	list score score_pct session seat, noobs
	regress score_pct setI_score_pct

	generate score_ratio = score_pct/100
	generate setI_score_ratio = setI_score_pct/100

	betareg score_ratio setI_score_ratio
	margins, dydx(*)

	betareg score_ratio CRTcorrtot
	margins, dydx(*)

	betareg score_ratio female
	margins, dydx(*)

	betareg score_ratio `demog'
	margins, dydx(*)

	use tmp, clear
	collapse (mean) apm_correct, by(apm_question)
	replace apm_correct = 100*apm_correct
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)
	twoway (connected apm_correct apm_question, sort mcolor(red) msymbol(point) lcolor(red) lwidth(thick)), ytitle("Percent" "Correct") ytitle(, size(medlarge) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medlarge) margin(medsmall)) xlabel(0(12)36) title(Average Correct RAPM Scores for Baseline, size(large)) subtitle("N = 55 undergraduate GSU students, paid $5 for task" "No effective time constraint", size(medsmall)) saving(figures/`appendix'raven_answers, replace)
	rename apm_correct apm_gsu
	sort apm_question
	save gsu, replace

	* export for Excel
	rename apm_question problem
	rename apm_gsu correct
	keep problem correct
	export excel using "GSU Average Baseline Scores.xlsx", replace firstrow(variables)

	use tmp, clear
	collapse (mean) apm_correct, by(session seat)
	replace apm_correct = 100*apm_correct
	mylabels 0(10)100, suffix(%) myscale(@) local(pct_a)
	mylabels 0(5)15, suffix(%) myscale(@) local(pct_s)
	twoway (histogram apm_correct, percent bin(15)), ytitle("Percent" "of" "Sample") ytitle(, orientation(horizontal) margin(medium)) ylabel(`pct_s', angle(horizontal)) xtitle(Percent Correct) xtitle(, margin(medsmall)) xlabel(`pct_a') title(Distribution of Correct RAPM Scores for Baseline, size(large)) subtitle("N = 55 undergraduate GSU students, paid $5 for task" "No effective time constraint", size(medsmall)) saving(figures/`appendix'raven_dist, replace)

* end of $doBaseline
}


* read in the Gignac data
if "$doGignac" == "y" {

	* get the data
	use within_subjects_data_oct10_2017__scored_online_offline_winsor, clear
	des

	* look at raven scores and treatment
	tab apm_sum_time1
	tab apm_sum_time2
	tab apm_sum_time1 time1_money
	tab apm_sum_time2 time1_money
	tab time1_money
	tab time1_money, nolabel

	* question by question
	forvalues x=1/36 {
		generate apm_h_`x' = apm`x'_r if time1_money==0
		generate apm_r_`x' = apm`x'_r if time1_money==1
	}
	summ apm_*

	* quantile-quantile plot of total scores
	generate apm1_hyp = apm_sum_time1 if time1_money==0
	generate apm1_real = apm_sum_time1 if time1_money==1
	qqplot apm1_real apm1_hyp, xtitle("No Incentives") ytitle(, size(medlarge) orientation(horizontal) margin(medium)) ylabel(, angle(horizontal)) ytitle(Incentives) xtitle(, size(medlarge)) title("Quantile-Quantile Plot of Distribution of" "RAPM Scores With and Without Financial Incentives", size(large) span) subtitle("Source: G.E. Gignac, A Moderate Financial Incentive Can Increase Effort, But Not" "Intelligence Test Performance in Adult Volunteers, {it:British Journal of Psychology}, 109(3), 2018, 500-516" " " "Subjects from first exposure to the RAPM: 48 incentivized, 51 not incentivized", size(small) span margin(medsmall)) saving(figures/`appendix'Gignac1, replace)

	generate apm2_hyp = apm_sum_time2 if time1_money==0
	generate apm2_real = apm_sum_time2 if time1_money==1
	qqplot apm2_real apm2_hyp, xtitle("No Incentives") ytitle(, size(medlarge) orientation(horizontal) margin(medium)) ylabel(, angle(horizontal)) ytitle(Incentives) xtitle(, size(medlarge)) title("Quantile-Quantile Plot of Distribution of" "RAPM Scores With and Without Financial Incentives", size(large) span) subtitle("Source: G.E. Gignac, A Moderate Financial Incentive Can Increase Effort, But Not" "Intelligence Test Performance in Adult Volunteers, {it:British Journal of Psychology}, 109(3), 2018, 500-516" " " "Subjects from second exposure to the RAPM: 51 incentivized, 48 not incentivized", size(small) span margin(medsmall)) saving(figures/`appendix'Gignac2, replace)

	* save for restarts
	save tmp, replace

	* set up data for analysis
	keep id age gender apm_*
	generate int obs = _n
	reshape long apm_h_ apm_r_, i(obs) j(apm_question)
	rename apm_h_ apm_0
	rename apm_r_ apm_1
	replace obs = _n
	reshape long apm_, i(obs) j(incentive)
	rename apm_ apm_correct
	tab apm_correct incentive, missing
	des

	egen int sid = group(id)
	generate int female = 0
	replace female = 1 if gender == 2
	generate age17 = age-17
	label variable sid "Subject ID"
	label variable female "Female"
	label variable age17 "Age over 17"
	label variable incentive "Financial incentive"
	label variable apm_correct "Correct answer on RAPM question"
	des

	* average scores
	use tmp, clear
	keep apm_*
	collapse (mean) apm_*
	generate int obs = _n
	reshape long apm_h_ apm_r_, i(obs) j(apm_question)
	rename apm_h_ apm_h
	rename apm_r_ apm_r
	replace apm_r = 100*apm_r
	replace apm_h = 100*apm_h
	twoway (connected apm_r apm_question, sort mcolor(black) msymbol(point) lcolor(black)) (connected apm_h apm_question, sort mcolor(blue) msymbol(point) lcolor(blue)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(RAPM Question) xtitle(, size(medium)) xlabel(0(12)36) title("Average Correct RAPM Scores by Incentive Treatment", size(large) margin(medium)) subtitle("Source: G.E. Gignac, A Moderate Financial Incentive Can Increase Effort," "But Not Intelligence Test Performance in Adult Volunteers," "{it:British Journal of Psychology}, 109(3), 2018, 500-516", size(small) margin(small)) legend(order(1 "Incentives" 2 "No Incentives") rows(2) position(7) ring(0)) saving(figures/`appendix'Gignac_averages, replace)

	* merge
	sort apm_question
	merge 1:1 apm_question using gsu_baseline
	drop _merge

	* comparisons
	twoway (connected apm_gsu apm_question, sort mcolor(red) msymbol(point) lcolor(red)) (connected apm_r apm_question, sort mcolor(black) msymbol(point) lcolor(black)) (connected apm_h apm_question, sort mcolor(blue) msymbol(point) lcolor(blue)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(RAPM Question) xtitle(, size(medium)) xlabel(0(12)36) title("Average Correct RAPM Scores", size(large) margin(medium)) subtitle("Source for Non-GSU data: G.E. Gignac, A Moderate Financial Incentive" "Can Increase Effort, But Not Intelligence Test Performance in Adult Volunteers," "{it:British Journal of Psychology}, 109(3), 2018, 500-516", size(small) margin(medsmall)) legend(order(1 "GSU No Incentives" 2 "Incentives" 3 "No Incentives") rows(3) position(7) ring(0)) saving(figures/`appendix'Gignac_rapm_averages, replace)
	twoway (connected apm_gsu apm_question, sort mcolor(red) msymbol(point) lcolor(red) lwidth(thick)) (connected apm_h apm_question, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medium)) xlabel(0(12)36) title("Raven Scores with No Salient Rewards:" "Comparison with Gignac [2018]", size(vlarge)) subtitle("Source for Non-GSU data: G.E. Gignac, A Moderate Financial Incentive" "Can Increase Effort, But Not Intelligence Test Performance in Adult Volunteers," "{it:British Journal of Psychology}, 109(3), 2018, 500-516", size(small) margin(medsmall)) legend(order(1 "GSU No Incentives" 2 "Non-GSU No Incentives") rows(2) position(7) ring(0)) saving(figures/`appendix'Gignac_comparison, replace)

	* save for pooled comparison picture
	rename apm_h apm_gignac_h
	sort apm_question
	keep apm_question apm_gignac_h
	save tmp_gignac.dta, replace

	* clean up
	erase tmp.dta

* end of $doGignac
}


* read in the Arthur and Day data
if "$doArthurDay" == "y" {

	* get the data
	import excel using "Arthur and Day.xlsx", firstrow clear
	des
	rename question apm_question
	generate apm_ad = 100*correct

	* merge in GSU data
	sort apm_question
	merge 1:1 apm_question using gsu_baseline
	drop _merge

	* comparisons
	twoway (connected apm_gsu apm_question, sort mcolor(red) msymbol(point) lcolor(red) lwidth(thick)) (connected apm_ad apm_question, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medium)) xlabel(0(12)36) title("Raven Scores with No Salient Rewards:" "Comparison with Arthur and Day [1994]", size(vlarge)) subtitle("Source for Non-GSU data: W. Arthur, Jr., and D.V. Day," "Development of a Short Form for the Raven Advanced Progressive Matrices Test," "{it:Educational and Psychological Measurement}, 54(2), Summer 1994, 394-403", size(small) margin(medsmall)) legend(order(1 "GSU No Incentives" 2 "Non-GSU No Incentives") rows(2) position(7) ring(0)) saving(figures/`appendix'ArthurDay_comparison, replace)

	* merge in Gignac data
	sort apm_question
	merge 1:1 apm_question using tmp_gignac.dta
	drop _merge

	* comparisons
	twoway (connected apm_gsu apm_question, sort mcolor(black) msymbol(point) lcolor(black) lwidth(thick)) (connected apm_ad apm_question, sort lpattern(solid) mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)) (connected apm_gignac_h apm_question, sort lpattern(solid) mcolor(red) msymbol(point) lcolor(red) lwidth(thick)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medium)) xlabel(0(12)36) title("Figure 2: Raven Scores with No Salient Rewards", size(vlarge)) subtitle("Non-GSU data from W. Arthur, Jr., and D.V. Day," "Development of a Short Form for the Raven Advanced Progressive Matrices Test," "{it:Educational and Psychological Measurement}, 54(2), Summer 1994, 394-403," "and" "G.E. Gignac, A Moderate Financial Incentive" "Can Increase Effort, But Not Intelligence Test Performance in Adult Volunteers," "{it:British Journal of Psychology}, 109(3), 2018, 500-516", size(small) margin(medsmall)) legend(order(1 "GSU (N=55)" 2 "Arthur and Day (N=202)" 3 "Gignac (N=99)") rows(3) position(7) ring(0) size(small)) saving(figures/`appendix'figure2, replace)
	gr export figures/`appendix'figure2.png, replace
	twoway (connected apm_gsu apm_question, sort mcolor(black) msymbol(point) lcolor(black) lwidth(thick)) (connected apm_ad apm_question, sort lpattern(shortdash) mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)) (connected apm_gignac_h apm_question, sort lpattern(longdash) mcolor(red) msymbol(point) lcolor(red) lwidth(thick)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medium)) xlabel(0(12)36) title("Figure 2: Raven Scores with No Salient Rewards", size(vlarge)) subtitle("Non-GSU data from W. Arthur, Jr., and D.V. Day," "Development of a Short Form for the Raven Advanced Progressive Matrices Test," "{it:Educational and Psychological Measurement}, 54(2), Summer 1994, 394-403," "and" "G.E. Gignac, A Moderate Financial Incentive" "Can Increase Effort, But Not Intelligence Test Performance in Adult Volunteers," "{it:British Journal of Psychology}, 109(3), 2018, 500-516", size(small) margin(medsmall)) legend(order(1 "GSU (N=55)" 2 "Arthur and Day (N=202)" 3 "Gignac (N=99)") rows(3) position(7) ring(0) size(small)) saving(figures/`appendix'figure2_, replace)
	twoway (connected apm_gsu apm_question, sort mcolor(black) msymbol(point) lcolor(black) lwidth(thick)) (connected apm_ad apm_question, sort lpattern(solid) mcolor(gs9) msymbol(point) lcolor(gs9) lwidth(thick)) (connected apm_gignac_h apm_question, sort lpattern(solid) mcolor(gs12) msymbol(point) lcolor(gs12) lwidth(thick)), ytitle("Percent" "Correct") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medium)) xlabel(0(12)36) title("Figure 2: Raven Scores with No Salient Rewards", size(vlarge)) subtitle("Non-GSU data from W. Arthur, Jr., and D.V. Day," "Development of a Short Form for the Raven Advanced Progressive Matrices Test," "{it:Educational and Psychological Measurement}, 54(2), Summer 1994, 394-403," "and" "G.E. Gignac, A Moderate Financial Incentive" "Can Increase Effort, But Not Intelligence Test Performance in Adult Volunteers," "{it:British Journal of Psychology}, 109(3), 2018, 500-516", size(small) margin(medsmall)) legend(order(1 "GSU (N=55)" 2 "Arthur and Day (N=202)" 3 "Gignac (N=99)") rows(3) position(7) ring(0) size(small)) saving(figures/`appendix'figure2_bw, replace)

* end of $doGignac
}


* read in the main, computerized data
if "$doData" == "y" {

	* generate some dummy questions as fillers
	clear
	set obs 300
	generate int subject = _n
	forvalues x=1/36 {
		generate int q`x' = `x'
	}
	reshape long q, i(subject) j(question)
	drop q
	drop if subject>50
	generate int session = 0
	generate int iteration = 0
	sort subject question
	list if subject<=2, noobs sepby(subject)
	save filler, replace

	* read in the baseline scores (generated earlier in code)
	import excel using "GSU Average Baseline Scores.xlsx", firstrow clear
	rename problem question
	rename correct baseline_correct
	sort question
	save baseline_correct, replace

	* read the computerized sessions
	import excel using "Raven Results 4.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = 4
	generate int period = question
	drop r9 r10

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r4, replace

	import excel using "Raven Results 5.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = 5
	generate int period = question
	drop r9 r10

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r5, replace

	import excel using "Raven Results 6.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = 6
	generate int period = question
	drop r9 r10

	tab question subject
	sort subject question
	list, noobs

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r6, replace

	import excel using "Raven Results 7.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = 7
	generate int period = question
	drop r9 r10

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r7, replace

	import excel using "Raven Results 8.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = 8
	generate int period = question
	drop r9 r10

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r8, replace

	import excel using "Raven Results 9.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = 9
	generate int period = question
	drop r9 r10

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r9, replace

	* now the 2022 data -- has multiple sessions, but with raven_session already defined
	import excel using "Raven 2022 Results.xlsx", sheet("Data") firstrow clear
	des
	list
	generate int session = raven_session
	drop raven_session
	drop r9 r10

	* drop session 10 -- incorrect stimuli
	drop if session == 10

	* fillers
	summ subject
	local nsub = r(max)
	di "There are `nsub' subjects"
	append using filler
	duplicates tag subject question, generate(filler)
	tab filler
	sort subject question iteration
	drop if filler == 2 & session == 0
	drop if filler == 1 & session == 0
	drop if subject > `nsub'
	tab question subject, missing

	save r10, replace

	* collate
	use r4, clear
	forvalues x=5/10 {
		append using r`x'
		erase r`x'.dta
	}
	erase r4.dta
	compress

	* read in the baseline correct
	sort question
	merge m:1 question using baseline_correct
	drop _merge
	label variable baseline_correct "Average baseline correct answers for each question"

	* define the rules
	generate int rule_dist2 = 0
	foreach x of local dist2 {
		replace rule_dist2 = 1 if question == `x'
	}
	label variable rule_dist2 "Questions that requires the distribution-of-two rule"

	generate int rule_dist3 = 0
	foreach x of local dist3 {
		replace rule_dist3 = 1 if question == `x'
	}
	label variable rule_dist3 "Questions that requires the distribution-of-three rule"

	generate int rule_addsub = 0
	foreach x of local addsub {
		replace rule_addsub = 1 if question == `x'
	}
	label variable rule_addsub "Questions that requires the distribution-of-two rule"

	generate int rule_pairwise = 0
	foreach x of local pairwise {
		replace rule_pairwise = 1 if question == `x'
	}
	label variable rule_pairwise "Questions that requires the distribution-of-two rule"


	* define the short-form questions
	generate int arthur_day = 0
	foreach x of local arthur_day {
		replace arthur_day = 1 if question == `x'
	}
	generate int bors_stokes = 0
	foreach x of local bors_stokes {
		replace bors_stokes = 1 if question == `x'
	}
	label variable arthur_day "12-item Short Form version of RAPM due to Arthur and Day [1994]"
	label variable bors_stokes "12-item Short Form version of RAPM due to Bors and Stokes [1998]"

	* generate a dummy for new data
	generate int data2022 = 0
	replace data2022 = 1 if session >= 10
	label variable data2022 "Data collected in 2022"

	* link to characteristics

	* SPS-EEC-XLAB17-343
	* 12345678901234
	generate seat = substr(computer,13,2)
	destring seat, replace
	tab seat, missing

	tab session, missing
	generate str10 Session = "raven-" + string(session)
	compress Session
	tab Session, missing

	* merge in the 2019 demographics
	sort Session seat
	merge m:1 Session seat using tmp_risklink
	tab Session _merge
	drop _merge

	* merge in the 2022 demographics
	tab session female, missing
	sort session seat
	tab seat if session==11, missing
	tab seat session, missing

	* update is needed, since demographics are in place but missing
	merge m:1 session seat using demog_2022, update

	* drop session 10 -- incorrect stimuli
	drop if session == 10

	* look at merge
	tab _merge
	tab Session _merge, nolabel missing
	tab session female, missing
	drop _merge

	generate int r9=.
	generate int r10=.

	tab subject session, missing
	drop if subject == . & session == .
	save tmp, replace

	* append from baseline
	use raven_gsu_baseline, clear
	rename apm_question question
	rename apm_correct accuracy
	rename correct correct_answer
	forvalues x=1/10 {
		generate int r`x'=.
}
	forvalues x=1/8 {
		replace r`x' = 0
		replace r`x' = 80 if answer == `x'
	}
	list answer r1-r10, noobs
	drop answer
	rename seat subject
	generate int iteration = 1
	tab subject session, missing
	save baseline_add, replace

	* append
	use tmp, clear
	append using baseline_add
	erase baseline_add.dta
	erase tmp.dta

	* unique, local subject ID
	egen int sid = group(session subject)
	tab sid, missing
	label variable sid "Unique subject ID"
	tab seat session if sid==109

	* zero tokens and earnings if someone did not submit
	forvalues x=1/8 {
		replace r`x' = 0 if session==0
	}
	replace earnings = 0 if session==0

	* session data (default to 80 tokens, progressive, constant incentives and 90-minute time allowed)
	generate int tokens = 80
	replace tokens = 1 if session == 1
	replace tokens = 1 if session == 2
	replace tokens = 1 if session == 3
	replace tokens = 1 if session == 7
	replace tokens = 1 if session == 16
	replace tokens = 1 if session == 17
	replace tokens = 1 if session == 18
	replace tokens = 1 if session == 19
	replace tokens = 1 if session == 20
	replace tokens = 1 if session == 21
	replace tokens = 1 if session == 25
	replace tokens = 1 if session == 26
	replace tokens = 1 if session == 27
	label variable tokens "Number of tokens"

	generate int time = 90
	replace time = 40 if session == 6
	label variable time "Time allowed"

	generate int incentives = 1
	label variable incentives "Incentives for correct answers provided"

	generate int increasing = 0
	replace increasing = 1 if session == 8
	label variable increasing "Increasing financial incentives"

	generate int scrambled = 0
	label variable scrambled "Order of problems are random"
	foreach x in 12 13 14 15 18 19 20 21 25 {
		replace scrambled = 1 if session==`x'
	}

	generate int progressive = 0
	label variable progressive "Order of problems are progressive"
	forvalues x=1/11 {
		replace progressive = 1 if session==`x'
	}
	foreach x in 16 17 26 27 {
		replace progressive = 1 if session==`x'
	}

	label variable responseTimeMs "Response time in milliseconds"
	generate timeS = responseTimeMs/1000
	generate timeM = timeS/60
	label variable timeS "Response time in seconds"
	label variable timeM "Response time in minutes"
	format timeS %10.0f
	format timeM %10.1f
	summ timeM

	* dummy for baseline treatment
	generate int baseline = 0
	replace baseline = 1 if session>=1 & session <=3
	label variable baseline "Baseline pen-and-paper, non-salient task"

	* dummy for time constraint
	generate int time_constraint = 0
	replace time_constraint = 1 if time < 90
	label variable time_constraint "Binding time constraint of 40 minutes"

	* dummy for one-token
	generate int one_token = 0
	replace one_token = 1 if tokens == 1
	label variable one_token "One token only, or forced to pick one answer"

	* dummy for one-token pen & pencil
	generate int one_tokenPP = 0
	replace one_tokenPP = 1 if session >=1 & session <=3
	label variable one_tokenPP "One token only, or forced to pick one answer, and pen and paper"

	* dummy for one-token pen & computerized
	generate int one_token_c = 0
	replace one_token_c = 1 if one_token == 1 & one_tokenPP == 0
	label variable one_token_c "One token only and computerized"

	* dummy for one-token pen & progressive
	generate int one_tokenP = 0
	replace one_tokenP = 1 if one_token==1 & scrambled == 0
	label variable one_tokenP "One token only and Progressive order of presentation"

	* dummy for one-token pen & scambled
	generate int one_tokenS = 0
	replace one_tokenS = 1 if one_token==1 & scrambled == 1
	label variable one_tokenS "One token only and Scrambled order of presentation"
	
	* dummy for standard treatment
	generate int standard = 0
	replace standard = 1 if session>=4 & one_token==0 & time==90 & increasing==0 & scrambled==0
	label variable standard "Standard treatment with QSR, using Progressive order, constant incentives and no effective time constraint"

	* be sure to just flag the last iteration
	tab session iteration, missing
	replace iteration = 1 if iteration == .
	bysort sid question: egen int maxit = max(iteration)
	drop if maxit == .
	tab iteration maxit
	sort session subject question iteration
	list sid session subject question iteration maxit earnings if maxit>1, sepby(session subject question) noobs

	* just retain the last iteration
	keep if iteration == maxit

	* token allocations
	label variable accuracy "Share of tokens allocated to the correct answer"
	label variable earnings "Earnings"
	summ accuracy

	* normalize earnings
	generate max_earn = alpha + beta
	generate earningsN = earnings/max_earn
	label variable earningsN "Earnings as a fraction of the maximum if 100% correct"

	* normalize earnings to uniform report (hardwire in 8 bins)
	generate earningsU = alpha + (beta/8)
	generate earningsNU = (earnings-earningsU)/max_earn
	label variable earningsNU "Earnings above Diffuse Earnings as a fraction of the maximum if 100% correct"
	summ earningsN earningsNU

	* delimit incentives
	replace incentives = 0 if session >=1 & session <= 3

	* blocks for set II of RAPM
	generate int block1 = 0
	replace block1 = 1 if question >= 1 & question <= 12
	generate int block2 = 0
	replace block2 = 1 if question >= 13& question <= 24
	generate int block3 = 0
	replace block3 = 1 if question >= 25 & question <= 36

	* drop session 0 trials
	drop if session == 0

	* get age as over 18 (17 to 56)
	replace age = age-18

	* labels for gender and race
	label define Lfemale 0 "Male" 1 "Female"
	label values female Lfemale

	label define Lblack 0 "Non-Black" 1 "Black"
	label values black Lblack

	* save
	compress
	save GSU_raven_main, replace

* end of $doData
}

* set up for beliefs to be recovered
if "$doRECOVERY" == "y" {

	* save Raven belief estimates for Stata processing
	use GSU_raven_main, clear
	generate str20 qid = ""
	generate str99 bquestion = ""
	forvalues x=1/36 {
		replace qid = "question`x'" if question==`x'
		replace bquestion = "What is the correct answer for question #`x'?" if question==`x'
	}
	compress qid bquestion
	tab qid, missing
	tab bquestion, missing

	forvalues x=1/8 {
		generate v_lo`x' = `x'
		generate v_hi`x' = `x'
	}

	forvalues x=1/8 {
		generate double choice`x' = r`x'/80
	}
	summ choice*

	generate int nbin = 8

	* just the data needed
	global pointers "tokens time timeM timeS incentives increasing one_token* scrambled data2022 standard baseline time_constraint baseline_correct accuracy earningsN raven_score CRTheurtot CRTcorrtot CRTwrongtot rule_dist2 rule_dist3 rule_addsub rule_pairwise arthur_day bors_stokes"
	keep LinkID raven_session session month day seat qid choice* bquestion $demog_recovery nbin alpha beta sid $pointers
	generate int risk_task=0
	generate int belief_task=1
	sort LinkID
	cd "Recover Beliefs -- Raven"
		save raven_beliefs_recovery, replace
	cd ..

}

* set up for recovered beliefs to be used
if "$getRECOVERED" == "y" {

	cd "Recover Beliefs -- Raven"
		use raven_summary, clear
		keep uid question p_mean_* r eta phi
	cd ..
	compress
	drop if question==.
	sort uid question
	save recovered_raven_beliefs, replace
	des

	* link to data set and rename
	use GSU_raven_main, clear

	* UID for 2019 data
	generate int uid = LinkID
	label variable uid "Unique ID linked to LinkID from DNH sessions"

	* UID for 2022 data
	generate int uid2022 = (raven_session*100)+seat
	replace uid = uid2022 if data2022==1
	drop uid2022
	drop if question==.

	* merge
	sort uid question
	merge 1:1 uid question using recovered_raven_beliefs
	tab _merge
	drop if _merge==1
	drop _merge

	* replace reports with recovered beliefs
	forvalues x=1/8 {
		rename p_mean_`x' b`x'
	}

	* check differences
	summ r1-r8 b1-b8

	* replace with recovered belief, saving reports as report*
	forvalues x=1/8 {
		rename r`x' report`x'
		rename b`x' r`x'
	}

	* save new data set
	save GSU_raven_main_recovered, replace

}
else {

	* generate uid if not done, to get the risk parameters
	use GSU_raven_main, clear

		* UID for 2019 data
		generate int uid = LinkID
		label variable uid "Unique ID linked to LinkID from DNH sessions"

		* UID for 2022 data
		generate int uid2022 = (raven_session*100)+seat
		replace uid = uid2022 if data2022==1
		drop uid2022

		drop if question==.

		* merge
		sort uid question
		merge 1:1 uid question using recovered_raven_beliefs
		tab _merge
		drop if _merge==1
		drop _merge

		* get reports
		forvalues x=1/8 {
			generate report`x' = r`x'
		}

		save GSU_raven_main2, replace


}
* end of $getRECOVERED

* analysis

	* get the N per treatment
	if "$useRECOVERED" == "y" {
		local file = "_recovered"
		local appendix = "B"
		local extra_text = "Beliefs recovered from reports, based on estimated risk preferences of individual"
	}
	else {
		local file = "2"
		local appendix = ""
		local extra_text = "Beliefs assumed to be the same as elicited reports"
		local extra_text = ""
	}
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	compress

	tab sid
	local Nsub = r(r)

	tab sid session if session>0 & session<=3
	local n_baseline = r(r)
	di "N: baseline is `n_baseline'"

	tab sid session if session>=4 & one_token==0 & time==90 & increasing==0 & progressive==1
	local n_standard = r(r)
	local n_progressive80 = r(r)

	tab sid session if session>=4 & one_token==1 & time==90 & increasing==0 & progressive==1
	local n_one_token = r(r)
	local n_progressive1 = r(r)

	tab sid session if session>=4 & one_token==0 & time==90 & increasing==0 & scrambled==1
	local n_standard_s = r(r)
	local n_scrambled80 = r(r)

	tab sid session if session>=4 & one_token==1 & time==90 & increasing==0 & scrambled==1
	local n_one_token_s = r(r)
	local n_scrambled1 = r(r)
	di "N: progressive1 is `n_progressive1', progressived80 is `n_progressive80'"
	di "N: scrambled1 is `n_scrambled1', scrambled80 is `n_scrambled80'"

	tab sid if session>=4 & one_token==0 & time==90 & increasing==1
	local n_increasing = r(r)

	tab sid if session>=4 & one_token==0 & time==40 & increasing==0
	local n_time_constraint = r(r)

	* get locals for the correct answers
	import excel using "GSU Raven Data -- Baseline 1.xlsx", sheet("Correct") firstrow clear
	des
	list
	forvalues x=1/36 {
		summ correct if question==`x'
		local s = r(mean)
		local c`x' = "`s'"
	}


* welfare cost of being constrained to report the mode

	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if scrambled==1 | scrambled==0
	drop if increasing == 1
	keep if time == 90
	keep if tokens == 80

	* keep variables we need
	keep uid question r eta phi alpha beta report* r1-r8 scrambled female black

	* one record per subject
	bysort uid: egen int record = seq()

	* generate earnings
	generate sum_reports = 0
	forvalues x=1/8 {
		replace sum_reports = sum_reports + (report`x'^2)
	}
	local max_report = 1
	forvalues x=1/8 {
		generate qsr_pay`x' = alpha + beta * ((2*report`x') - sum_reports)
		generate max_pay`x' = alpha + beta * ((2*`max_report') - `max_report')
	}
	summ alpha beta qsr_pay* max_pay*

	* crra in 1-r form
	generate crra = 1-r

	* EU of report, but with true beliefs (from RDU, but use these for now)
	generate eu_report = 0
	forvalues x=1/8 {
		replace eu_report = eu_report + (r`x' * ((qsr_pay`x'^crra)/crra))
	}

	* CE of report
	generate ce_report_eut = (eu_report * crra)^(1/crra)

	* answer with modal belief
	egen max_belief = rowmax(r1 r2 r3 r4 r5 r6 r7 r8)
	generate maximum_pay = 0
	forvalues x=1/8 {
		replace maximum_pay = qsr_pay`x' if r`x' == max_belief
	}
	summ maximum_pay

	* get ranks
	list uid question qsr_pay* in 1/30, noobs
	reshape long qsr_pay, i(uid question) j(bin)
	list uid question bin qsr_pay in 1/20, noobs sepby(question)
	bysort uid question: egen rank_pay = rank(-qsr_pay), unique 
	list uid question bin qsr_pay rank_pay in 1/20, noobs sepby(question)
	reshape wide qsr_pay rank_pay, i(uid question) j(bin)
	list uid question qsr_pay* rank_pay* in 1/36, noobs

	* EU if forced to report mode, even if only expecting it with belief r`x': add the 0 at the end for exposition... :) 
	generate eu_mode = 0
	forvalues x=1/8 {
		replace eu_mode = eu_mode + (r`x' * ((qsr_pay`x'^crra)/crra)) if maximum_pay==qsr_pay`x' & eu_mode==0
	}

	* CE of being forced to report mode
	generate ce_mode_eut = (eu_mode * crra)^(1/crra)

	* welfare cost in CE of being forced to report the mode
	generate w_cost = ce_report_eut - ce_mode_eut
	generate w_cost_pct = 100*(w_cost/ce_report_eut)
	format w_cost_pct %6.1f

	sort uid question
	summ eu_report eu_mode ce_report_eut ce_mode_eut w_cost w_cost_pct if scrambled==0 & record==1
	summ eu_report eu_mode ce_report_eut ce_mode_eut w_cost w_cost_pct if scrambled==1 & record==1
	drop max_pay*

	* this is an easy question, and should have a low welfare cost
	summ eu_report eu_mode ce_report_eut ce_mode_eut w_cost w_cost_pct if question==1

	* this is a hard question, and should have a high welfare cost
	summ eu_report eu_mode ce_report_eut ce_mode_eut w_cost w_cost_pct if question==36

	* see some specific subject and 5 responses
	list uid question r1-r8 qsr_pay1 qsr_pay2 qsr_pay3 qsr_pay4 qsr_pay5 qsr_pay6 qsr_pay7 qsr_pay8 maximum_pay in 1/5, noobs
	list uid question r1-r8 eu_report eu_mode ce_report_eut ce_mode_eut w_cost w_cost_pct r eta phi crra in 1/5, noobs

	* RDU: y = (exp( (-`eta') * (-ln(x) )^`phi'))

	compress rank_pay*
	list uid question qsr_pay* rank_pay* in 1/5, noobs
	forvalues x = 1/8 {
		generate rank_prob`x' = 0
		generate rank_payoff`x' = 0
		forvalues xx = 1/8 {
			replace rank_prob`x' = r`xx' if rank_pay`xx' == `x'
			replace rank_payoff`x' = qsr_pay`xx' if rank_pay`xx' == `x'
		}
	}
	summ rank*
	list uid question qsr_pay* rank_pay1 rank_pay2 rank_pay3 rank_pay4 rank_pay5 rank_pay6 rank_pay7 rank_pay8 rank_payoff1 rank_payoff2 rank_prob1 rank_prob2 rank_prob3 in 1/5, noobs

	generate dw1 = rank_prob1
	replace dw1 = exp((-eta)*(-ln(rank_prob1))^phi) if rank_prob1>0 & rank_prob1<1
	generate decum_prob1 = rank_prob1
	forvalues x=2/8 {
		local xx= `x'-1	
		generate decum_prob`x' = decum_prob`xx'+rank_prob`x'
		generate dw`x' = decum_prob`x'
		replace dw`x' = exp((-eta)*(-ln(decum_prob`x'))^phi) if decum_prob`x'>0 & decum_prob`x'<1
	}
	summ decum* dw*

	* now the decrement in dw
	generate ddw1 = dw1
	forvalues x=2/8 {
		local xx= `x'-1	
		generate ddw`x' = dw`x' - dw`xx'
	}
	summ dw* ddw*

	* RDU of report, but with the decision weights of true beliefs
	generate rdu_report = 0
	forvalues x=1/8 {
		replace rdu_report = rdu_report + (ddw`x' * ((rank_payoff`x'^crra)/crra))
	}
	summ eu_report rdu_report

	* CE of report
	generate ce_report_rdu = (rdu_report * crra)^(1/crra)

	* RDU if forced to report mode 
	generate rdu_mode = 0
	forvalues x=1/8 {
		replace rdu_mode = rdu_mode + (ddw`x' * ((rank_payoff`x'^crra)/crra)) if maximum_pay==rank_payoff`x' & rdu_mode==0
	}

	* CE of being forced to report mode
	generate ce_mode_rdu = (rdu_mode * crra)^(1/crra)

	* welfare cost in CE of being forced to report the mode
	generate w_cost_rdu = ce_report_rdu - ce_mode_rdu
	generate w_cost_rdu_pct = 100*(w_cost_rdu/ce_report_rdu)
	format w_cost_rdu_pct %6.1f

	sort uid question
	summ w_* if scrambled==0 & record==1
	summ w_* if scrambled==1 & record==1
	bysort female: summ w_cost_rdu_pct if scrambled==1 & record==1
	bysort black: summ w_cost_rdu_pct if scrambled==1 & record==1
	bysort question: summ w_cost_rdu_pct if scrambled==1

	list uid question r1-r8 w_cost_rdu_pct in 1/108, noobs sepby(uid)

	* save the welfare calculations
*	save raven_welfare, replace

* calculations
pwcorr w_cost_rdu w_cost_rdu_pct

* RDU welfare cost 
generate w = w_cost_rdu
summ w if uid == 21347, detail
bysort uid: egen W = total(w)
summ w W if uid == 21347

bysort scrambled: summ w
bysort scrambled: summ w if question<=20
bysort scrambled: summ w if question>20
bysort scrambled: summ w if question>30

bysort scrambled: summ W if record==1

bysort female: summ W if scrambled==0 & record==1
bysort black: summ W if scrambled==0 & record==1

* displays
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)
	twoway (lpolyci w_cost_rdu_pct question if scrambled==1, lcolor(green) clwidth(thick) clpattern(solid) fcolor(green) fintensity(*0.3) alwidth(none)) (lpolyci w_cost_rdu_pct question if scrambled==0, lcolor(blue) clwidth(thick) fcolor(blue) fintensity(*0.3) alwidth(none)), ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, margin(medsmall)) xlabel(1 10 20 30 36) title("Figure B1: Expected Welfare Cost to Respondent" "of Being Required to Only Report Modal Belief", size(vlarge)) subtitle("Evaluated using RDU risk preferences of each subject" "Using recovered beliefs from {bf:Eighty Tokens} task" "Kernel-weighted local polynomial plot with 95% confidence intervals", size(medsmall) margin(medium)) legend(order(1 "" 2 "Scrambled" 3 "" 4 "Progressive" ) position(11) ring(0)) saving(figures/raven_w_pct, replace)

	summ w
	summ W if record==1

	mylabels 0(0.25)1, prefix($) myscale(@) local(doll)
	twoway (lpolyci w question if scrambled==1, lcolor(green) clwidth(thick) clpattern(solid) fcolor(green) fintensity(*0.3) alwidth(none)) (lpolyci w question if scrambled==0, lcolor(blue) clwidth(thick) fcolor(blue) fintensity(*0.3) alwidth(none)), ylabel(`doll', angle(horizontal)) xtitle(Question) xtitle(, margin(medsmall)) xlabel(1 10 20 30 36) title("Figure B1: Expected Welfare Cost to Respondent" "of Being Required to Only Report Modal Belief", size(vlarge)) subtitle("Evaluated using RDU risk preferences of each subject" "Using recovered beliefs from {bf:Eighty Tokens} task" "Kernel-weighted local polynomial plot with 95% confidence intervals", size(medsmall) margin(medium)) legend(order(1 "" 2 "Scrambled" 3 "" 4 "Progressive" ) position(11) ring(0)) saving(figures/raven_w_doll, replace)

	mylabels 0(0.5)2, prefix($) myscale(@) local(doll)
	twoway (lpolyci w question if scrambled==1, lcolor(green) clwidth(thick) clpattern(solid) fcolor(green) fintensity(*0.3) alwidth(none)) (lpolyci w question if scrambled==0, lcolor(blue) clwidth(thick) fcolor(blue) fintensity(*0.3) alwidth(none)), ylabel(`doll', angle(horizontal)) xtitle(Question) xtitle(, margin(medsmall)) xlabel(1 10 20 30 36) title("A. Welfare Cost per Question") legend(order(1 "" 2 "Scrambled" 3 "" 4 "Progressive" ) position(11) ring(0)) saving(w, replace)

	mylabels 0(10)72, prefix($) myscale(@) local(doll)
	twoway (kdensity W if record==1 & scrambled==0, lcolor(blue) lwidth(thick)) (kdensity W if record==1 & scrambled==1, lcolor(green) lwidth(thick)), ytitle("") ylabel(none) xtitle("Welfare Cost") xtitle(, margin(medsmall)) xlabel(`doll') title("B. Welfare Cost per Individual") legend(order(2 "Scrambled" 1 "Progressive") rows(2) position(1) ring(0)) saving(w_agg, replace)

	if "$useRECOVERED" == "y" {
		local appendix = ""
		gr combine w.gph w_agg.gph, rows(1) imargin(small) title("Figure `appendix'13: Expected Welfare Cost to Respondent" "of Being Required to Only Report Modal Belief", size(vlarge)) subtitle("Evaluated using RDU risk preferences of each subject" "Using recovered beliefs from {bf:Eighty Tokens} task", size(small)) saving(raven_`appendix'13.gph, replace)
		gr export raven_`appendix'13.png, replace
	}


* EUT welfare cost 
drop w W
generate w = w_cost
summ w if uid == 21347, detail
bysort uid: egen W = total(w)
summ w W if uid == 21347
bysort scrambled: summ W if record==1

* displays
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)
	twoway (lpolyci w_cost_rdu_pct question if scrambled==1, lcolor(green) clwidth(thick) clpattern(solid) fcolor(green) fintensity(*0.3) alwidth(none)) (lpolyci w_cost_rdu_pct question if scrambled==0, lcolor(blue) clwidth(thick) fcolor(blue) fintensity(*0.3) alwidth(none)), ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, margin(medsmall)) xlabel(1 10 20 30 36) title("Figure B1: Expected Welfare Cost to Respondent" "of Being Required to Only Report Modal Belief", size(vlarge)) subtitle("Evaluated using EUT risk preferences of each subject" "Using recovered beliefs from {bf:Eighty Tokens} task" "Kernel-weighted local polynomial plot with 95% confidence intervals", size(medsmall) margin(medium)) legend(order(1 "" 2 "Scrambled" 3 "" 4 "Progressive" ) position(11) ring(0)) saving(figures/raven_w_pct_eut, replace)

	summ w
	summ W if record==1

	mylabels 0(0.25)1, prefix($) myscale(@) local(doll)
	twoway (lpolyci w question if scrambled==1, lcolor(green) clwidth(thick) clpattern(solid) fcolor(green) fintensity(*0.3) alwidth(none)) (lpolyci w question if scrambled==0, lcolor(blue) clwidth(thick) fcolor(blue) fintensity(*0.3) alwidth(none)), ylabel(`doll', angle(horizontal)) xtitle(Question) xtitle(, margin(medsmall)) xlabel(1 10 20 30 36) title("Figure B1: Expected Welfare Cost to Respondent" "of Being Required to Only Report Modal Belief", size(vlarge)) subtitle("Evaluated using RDU risk preferences of each subject" "Using recovered beliefs from {bf:Eighty Tokens} task" "Kernel-weighted local polynomial plot with 95% confidence intervals", size(medsmall) margin(medium)) legend(order(1 "" 2 "Scrambled" 3 "" 4 "Progressive" ) position(11) ring(0)) saving(figures/raven_w_doll, replace)

	mylabels 0(0.5)2, prefix($) myscale(@) local(doll)
	twoway (lpolyci w question if scrambled==1, lcolor(green) clwidth(thick) clpattern(solid) fcolor(green) fintensity(*0.3) alwidth(none)) (lpolyci w question if scrambled==0, lcolor(blue) clwidth(thick) fcolor(blue) fintensity(*0.3) alwidth(none)), ylabel(`doll', angle(horizontal)) xtitle(Question) xtitle(, margin(medsmall)) xlabel(1 10 20 30 36) title("A. Welfare Cost per Question") legend(order(1 "" 2 "Scrambled" 3 "" 4 "Progressive" ) position(11) ring(0)) saving(w, replace)

	mylabels 0(10)72, prefix($) myscale(@) local(doll)
	twoway (kdensity W if record==1 & scrambled==0, lcolor(blue) lwidth(thick)) (kdensity W if record==1 & scrambled ==1, lcolor(green) lwidth(thick)), ytitle("") ylabel(none) xtitle("Welfare Cost") xtitle(, margin(medsmall)) xlabel(`doll') title("B. Welfare Cost per Individual") legend(order(2 "Scrambled" 1 "Progressive") rows(2) position(1) ring(0)) saving(w_agg, replace)

	if "$useRECOVERED" == "y" {
		local appendix = "B"
		gr combine w.gph w_agg.gph, rows(1) imargin(small) title("Figure `appendix'13: Expected Welfare Cost to Respondent" "of Being Required to Only Report Modal Belief", size(vlarge)) subtitle("Evaluated using EUT risk preferences of each subject" "Using recovered beliefs from {bf:Eighty Tokens} task", size(small)) saving(raven_`appendix'13.gph, replace)
		gr export raven_`appendix'13.png, replace
	}


* econometrics on accuracy compared to baseline
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	tab session tokens, missing
	replace tokens = 1 if session <=3
	keep if tokens == 1
	keep if increasing == 0
	keep if time == 90
	drop if data2022 == 1 & tokens == 80
	drop if scrambled==1

	collapse (sum) baseline_correct accuracy earningsN timeM (median) one_token session `demog', by(sid)
	tab session one_token
	replace one_token = 0 if session<=3
	tab session one_token

	replace baseline_correct = baseline_correct/100
	generate baseline_pct = 100*(baseline_correct/36)
	generate accuracy_pct = 100*(accuracy/36)
	generate earnings_pct = 100*(earningsN/36)

	generate baseline_ratio = baseline_pct/100
	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format baseline_pct accuracy_pct earnings_pct %8.0f

	* total effect
	fracreg probit accuracy_ratio one_token
	margins, dydx(*)

	* marginal effects
	fracreg probit accuracy_ratio one_token `demog'
	margins, dydx(*)
	matrix r = r(table)
	matrix list r
	local effect = r[1,1]
	local pvalue = r[4,1]
	di "First results of paper: effect of one-token w.r.t. baseline on Accuracy was `effect' with p-value `pvalue'"

	* check N
	tabulate one_token
	tabulate session one_token


* econometrics on accuracy and earnings of one-token compared to eight-tokens
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	keep if tokens==1 | tokens==80
	keep if incentives == 1
	drop if increasing == 1
	keep if time == 90
	drop if scrambled==1

	collapse (sum) accuracy earningsN timeM (median) one_token session `demog', by(sid)

	generate accuracy_pct = 100*(accuracy/36)
	generate earnings_pct = 100*(earningsN/36)

	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format accuracy_pct earnings_pct %8.0f

	generate int eighty_tokens = 0
	replace eighty_tokens = 1 if one_token == 0
	tab eighty_tokens one_token

	* total effect
	fracreg probit accuracy_ratio eighty_tokens
	margins, dydx(*)
	fracreg probit earnings_ratio eighty_tokens
	margins, dydx(*)

	* marginal effects
	fracreg probit accuracy_ratio eighty_tokens `demog'
	margins, dydx(*)
	matrix r = r(table)
	matrix list r
	local effect = r[1,1]
	local pvalue = r[4,1]
	di "Second result of paper: effect of eight-tokens w.r.t. one-token on Accuracy was `effect' with p-value `pvalue'"

	fracreg probit earnings_ratio eighty_tokens `demog'
	margins, dydx(*)
	matrix r = r(table)
	matrix list r
	local effect = r[1,1]
	local pvalue = r[4,1]
	di "Second result of paper: effect of eight-tokens w.r.t. one-token on Efficiency was `effect' with p-value `pvalue'"

	* check N
	tabulate eighty_tokens
	tabulate session eighty_tokens


* display the pattern of choices over short-form questions
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear


	keep if time_constraint==0
	keep if one_token==0
	keep if increasing==0
	keep if scrambled==0
	keep if standard==1
	keep if incentives==1
	keep if data2022==0

	keep session sid r1-r8 question arthur_day bors_stokes LinkID
	drop if sid==.
	drop if question==.
	reshape long r, i(sid question) j(bin)

	tab bors_stokes arthur_day

	summ bin if arthur_day==1
	tab arthur_day if bin==r(min) & bors_stokes==0
	tab LinkID if arthur_day==1

	summ bin if bors_stokes==1
	tab bors_stokes if bin==r(min) & arthur_day==0
	tab LinkID if bors_stokes==1

	* display over all short-form questions
	graph bar (mean) r if arthur_day==1, over(bin) bar(1, fcolor(blue)) ytitle("") by(, title("Figure D6: Distribution of Token Allocations" "by Original Raven Question in the 12-item" "Short Form due to Arthur and Day [1994]", size(vlarge) span) subtitle("Pooled over incentivized subjects, with no effective time constraint," "the Progressive presentation of questions, and constant incentives", span size(small)) note("")) by(question, noyrescale noxrescale noiyaxes noiytick noiylabel noiytitle) saving(figures/raven_D6, replace)
	gr export raven_D6.png, replace

	gr export raven_D6.png, replace
	graph bar (mean) r if bors_stokes==1, over(bin) bar(1, fcolor(blue)) ytitle("") by(, title("Figure D7: Distribution of Token Allocations" "by Original Raven Question in the 12-item" "Short Form due to Bors and Stokes [1998]", size(vlarge) span) subtitle("Pooled over incentivized subjects, with no effective time constraint," "the Progressive presentation of questions, and constant incentives", span size(small)) note("")) by(question, noyrescale noxrescale noiyaxes noiytick noiylabel noiytitle) saving(figures/raven_D7, replace)
	gr export raven_D7.png, replace


* pictures on increasing incentives
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
*	keep if session>=4
*	keep if tokens == 80
	keep if standard==1 | increasing == 1
	save tmp, replace

	collapse (median) increasing, by(sid)
	tab increasing, missing
	count if increasing==0
	local nB = r(N)
	count if increasing==1
	local nII = r(N)
	di "There are `nII' subjects in the increasing incentives condition, and `nB' in the baseline (should be `n_standard')" 

	use tmp, clear
	erase tmp.dta
	collapse (mean) baseline_correct accuracy earningsN, by(increasing question)
	replace accuracy = 100*accuracy
	replace earningsN = 100*earningsN
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)

	local color_c "*.25"
	local thick "thick"
	local lsize "medsmall"

	twoway (connected accuracy question if increasing==0, sort mcolor(red) msymbol(point) lcolor(red) lwidth(`thick')) (connected accuracy question if increasing==1, sort mcolor(red`color_c') msymbol(point) lcolor(red`color_c')  lwidth(`thick')), ytitle("") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medlarge)) xlabel(0(12)36) title(Accuracy, size(large)) legend(order(1 "Constant Incentives (N=`n_standard')" 2 "Increasing Incentives (N=`nII')") rows(2) position(7) ring(0) size(`lsize')) saving(tmp1, replace)
	twoway (connected earningsN question if increasing==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(`thick')) (connected earningsN question if increasing==1, sort mcolor(blue`color_c') msymbol(point) lcolor(blue`color_c')  lwidth(`thick')), ytitle("") ytitle(, size(medlarge) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medlarge)) xlabel(0(12)36) title(Efficiency, size(large)) legend(order(1 "Constant Incentives (N=`n_standard')" 2 "Increasing Incentives (N=`nII')") rows(2) position(7) ring(0) size(`lsize')) saving(tmp2, replace)

	gr combine tmp1.gph tmp2.gph, cols(2) ycommon imargin(vsmall) title("Figure D4: Effect of Increasing Financial Incentives" "on Accuracy and Efficiency", size(vlarge) span margin(small)) subtitle("Solely comparing results within the Progressive Treatment", margin(small) span) saving(figures/raven_D4, replace)
	gr export raven_D4.png, replace


* estimates of effects of increasing incentives
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	keep if standard==1 | increasing == 1
	collapse (sum) accuracy earningsN (median) increasing `demog', by(sid)

	generate int nquestions = 36
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format accuracy_pct earnings_pct %8.0f

	* marginal effects -- no interaction with working
	fracreg probit accuracy_ratio female##increasing age black##increasing business##increasing noreligion##increasing working##increasing if increasing==0 | increasing==1, vce(robust)
	margins female##increasing black##increasing business##increasing noreligion##increasing working##increasing, pwcompare(effects)

	fracreg probit earnings_ratio female##increasing age black##increasing business##increasing noreligion##increasing working##increasing if increasing==0 | increasing==1, vce(robust)
	margins female##increasing black##increasing business##increasing noreligion##increasing working##increasing, pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "c"
		local tt = 0
		foreach t of local demog {
			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on efficiency
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "c"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''

	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''
	
	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -10(5)35, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) title("Figure D5: Effects of Increasing Incentives on Efficiency", span size(vlarge)) subtitle("Average marginal effects of progressively increasing incentives from Fractional Regression" "Solely comparing results within the Progressive Treatment" "`nII' subjects with increasing incentives, and `n_standard' with constant incentives", span size(small) margin(small)) saving(figures/raven_D5, replace)
	gr export raven_D5.png, replace

* time distributions
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	keep if standard==1 | time_constraint == 1
	collapse (sum) timeS timeM (median) time_constraint, by(sid)

	tab time_constraint, missing
	count if time_constraint==0
	local nB = r(N)
	count if time_constraint==1
	local nTC = r(N)
	di "There are `nTC' subjects in the time-constraint condition, and `nB' in the baseline (should be `n_standard')" 

	summ timeM if time_constraint==0
	local m = r(mean)

	summ timeM if time_constraint==0, detail
	centile timeM if time_constraint==0, centile(1(1)99)

	summ timeM if time_constraint==1, detail
	centile timeM if time_constraint==1, centile(1(1)99)

	* showing 90 minutes
	twoway (kdensity timeM if time_constraint==0, recast(line) lcolor(black) lwidth(thick)) (kdensity timeM if time_constraint==1, recast(line) lcolor(black) lwidth(thick) lpattern(dash)),  yscale(off) xtitle(Time Required to Complete the Battery in Minutes) xtitle(, size(medium) margin(medium)) xline(20 40 90, lwidth(medium) lpattern(dash) lcolor(red)) xline(`m', lwidth(medium) lpattern(dash) lcolor(blue)) xlabel(0(10)95) title("Figure D1: Distribution of Time Taken", size(vlarge)) subtitle("Time taken to complete the Progressive 80 Tokens task" "with a 90-minute constraint or a 40-minute constraint", margin(medsmall)) note("") legend(order(1 "No Time Constraint (N=`n_standard')" 2 "With Time Constraint (N=`nTC')") rows(2) position(1) ring(0)) saving(figures/raven_D1, replace)
	gr export raven_D1.png, replace


* quick pictures on time constraints
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	keep if standard==1 | time_constraint == 1
	collapse (mean) baseline_correct accuracy earningsN (median) `demog', by(time_constraint question)
	replace accuracy = 100*accuracy
	replace earningsN = 100*earningsN
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)

	local color_c "*.25"
	local thick "thick"
	local lsize "medsmall"

	twoway (connected accuracy question if time_constraint==0, sort mcolor(red) msymbol(point) lcolor(red) lwidth(`thick')) (connected accuracy question if time_constraint==1, sort mcolor(red`color_c') msymbol(point) lcolor(red`color_c')  lwidth(`thick')), ytitle("") ytitle(, size(medium) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medlarge)) xlabel(0(12)36) title(Accuracy, size(large)) legend(order(1 "No Time Constraint (N=`n_standard')" 2 "With Time Constraint (N=`nTC')") rows(2) position(7) ring(0) size(`lsize')) saving(tmp1, replace)
	twoway (connected earningsN question if time_constraint==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(`thick')) (connected earningsN question if time_constraint==1, sort mcolor(blue`color_c') msymbol(point) lcolor(blue`color_c')  lwidth(`thick')), ytitle("") ytitle(, size(medlarge) orientation(horizontal)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medlarge)) xlabel(0(12)36) title(Efficiency, size(large)) legend(order(1 "No Time Constraint (N=`n_standard')" 2 "With Time Constraint (N=`nTC')") rows(2) position(7) ring(0) size(`lsize')) saving(tmp2, replace)
	gr combine tmp1.gph tmp2.gph, cols(2) ycommon imargin(vsmall) title("Figure D2: Effect of 40-Minute Time Constraint" "on Accuracy and Efficiency", size(vlarge) span margin(small)) saving(figures/raven_D2, replace)
	gr export raven_D2.png, replace
	erase tmp1.gph
	erase tmp2.gph
	
* estimates of effects of time constraints
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	keep if standard==1 | time_constraint == 1
	collapse (sum) accuracy earningsN (median) time_constraint `demog', by(sid)

	generate int nquestions = 36
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format accuracy_pct earnings_pct %8.0f

	* marginal effects -- no interaction with working
	fracreg probit accuracy_ratio female##time_constraint age black##time_constraint business##time_constraint noreligion##time_constraint working##time_constraint if time_constraint==0 | time_constraint==1, vce(robust)
	margins female##time_constraint black##time_constraint business##time_constraint noreligion##time_constraint working##time_constraint, pwcompare(effects)

	fracreg probit earnings_ratio female##time_constraint age black##time_constraint business##time_constraint noreligion##time_constraint working##time_constraint if time_constraint==0 | time_constraint==1, vce(robust)
	margins female##time_constraint black##time_constraint business##time_constraint noreligion##time_constraint working##time_constraint, pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "c"
		local tt = 0
		foreach t of local demog {
			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on efficiency
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "c"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''

	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''
	
	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -15(5)20, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) title("Figure D3: Effects of Time Constraint on Efficiency", span size(vlarge)) subtitle("Average marginal effects of 40-minute time constraint from Fractional Regression" "Solely comparing results within the Progressive Treatment" "`n_TC' subjects with a time-constraint, and `n_standard' unconstrained", span size(small) margin(small)) saving(figures/raven_D3, replace)
	gr export raven_D3.png, replace

* personality variables

	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if baseline == 0
	keep if standard==1 | one_token==1
	drop if scrambled==1

	* summarize BFI
	global bfi "bfi_extra bfi_agree bfi_cons bfi_neur bfi_open"
	bysort sid: egen int record=seq()
	summ $bfi if record==1

	tab one_token if record==1
	drop if bfi_extra == .
	tab one_token if record==1

* econometrics on accuracy and earnings compared to baseline
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if time == 90
	keep if increasing == 0

	collapse (sum) baseline_correct accuracy earningsN timeM (median) session baseline one_token_c one_token one_tokenPP one_tokenP one_tokenS standard incentives (median) raven_score CRT* `demog', by(sid)
	get_label

	replace baseline_correct = baseline_correct/100
	generate raven_setI = 100*(raven_score/12)
	generate baseline_pct = 100*(baseline_correct/36)
	generate accuracy_pct = 100*(accuracy/36)
	generate earnings_pct = 100*(earningsN/36)

	generate baseline_ratio = baseline_pct/100
	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format baseline_pct accuracy_pct earnings_pct %8.0f

	bysort baseline: summ accuracy_ratio if one_token==1 | baseline==1

	* look at set I comparison
	sort raven_setI accuracy_pct
	list sid accuracy_pct raven_setI if baseline==1, noobs sepby(raven_setI)
	bysort raven_setI: summ accuracy_pct if baseline==1

	* correlation with set I and CRT -- why so large?
	pwcorr accuracy_pct raven_setI CRT* if baseline==1

	* correct correlation of set I and CRT
	fracreg probit accuracy_ratio raven_setI if baseline==1
	margins, dydx(*)

	fracreg probit accuracy_ratio CRTcorrtot if baseline==1
	margins, dydx(*)

	* total effects, just baseline and one-token
	fracreg probit accuracy_ratio baseline if one_token==1
	margins, dydx(*)

	* check correlation of demographics and baseline
	summ accuracy_ratio baseline `demog' if one_token==1
	pwcorr accuracy_ratio baseline `demog' if one_token==1
	tab female baseline, missing
	tab female baseline if one_token==1, missing

	* marginal effects, just baseline and one-token
	fracreg probit accuracy_ratio baseline `demog' if one_token==1
	margins, dydx(*)


	* total effects, baseline compared to one_token + standard
	fracreg probit accuracy_ratio baseline
	margins, dydx(*)

	* marginal effects, baseline compared to one_token + standard
	fracreg probit accuracy_ratio baseline `demog'
	margins, dydx(*)

	* total effects, baseline compared to standard
	fracreg probit accuracy_ratio baseline if baseline==1 | standard==1
	margins, dydx(*)

	* marginal effects, baseline compared to standard
	fracreg probit accuracy_ratio baseline `demog' if baseline==1 | standard==1
	margins, dydx(*)

	* total effects of gender and race just for baseline
	fracreg probit accuracy_ratio female if baseline==1
	margins, dydx(*)
	fracreg probit accuracy_ratio black if baseline==1
	margins, dydx(*)

	* marginal demographics just for baseline (coefplot 1)
*	fracreg probit accuracy_ratio `demog' if baseline==1, het(`demog') vce(robust)
	fracreg probit accuracy_ratio `demog' if baseline==1, vce(robust)
*	betareg accuracy_ratio `demog' if baseline==1, vce(robust) link(probit) slink(log) scale(female black)
	margins, dydx(*)

	* save estimates for coefplot
		matrix rtable = r(table)
		matrix list rtable

		local c = "a"
		local tt = 0
		foreach t of local demog {
			local tt = `tt'+1
			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_p_`t' =  rtable[4,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing baseline accuracy
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "a"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel A: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel A: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel A: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel A: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	
	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	di "Third result of the paper, on demographics: panel A: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	matrix e[5,3] = ``c'_hi_`t''

	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -20(10)50, suffix(%) myscale(@) local(pct)
	mylabels -40(10)60, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') title("{bf:A. Accuracy:} Baseline", size(medlarge)) msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) saving(`c'_effects_, replace)

	summ `demog' if one_token_c==1 | baseline==1

	* total effects
	fracreg probit accuracy_ratio female##one_token_c if one_token_c==1 | baseline==1, vce(robust)
	margins female##one_token_c, pwcompare(effects)

	fracreg probit accuracy_ratio black##one_token_c if one_token_c==1 | baseline==1, vce(robust)
	margins black##one_token_c, pwcompare(effects)

	* marginal effects
	fracreg probit accuracy_ratio female##one_token_c age black##one_token_c business##one_token_c noreligion##one_token_c working##one_token_c if one_token_c==1 | baseline==1, vce(robust)
	margins female##one_token_c black##one_token_c business##one_token_c noreligion##one_token_c working##one_token_c, pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "b"
		local tt = 0
		foreach t of local demog {
			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_p_`t' =  rtable[4,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on accuracy
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "b"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel B: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel B: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel B: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel B: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	
	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel B: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -50(10)40, suffix(%) myscale(@) local(pct)
	mylabels -40(10)60, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') title("{bf:B. Accuracy:} One Token Progressive {it:versus} Baseline", size(medlarge)) msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) saving(`c'_effects_, replace)



* econometrics on accuracy and earnings with salient rewards
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	drop if standard == .
	drop if arthur_day == 1
	drop if bors_stokes == 1

	bysort sid: egen int record=seq()
	tab standard if record==1, missing
	tab increasing if record==1, missing
	tab time if record==1, missing

	generate int timeC = 0
	replace timeC = 1 if time==40
	label variable timeC "Time constrained to 40 minutes"
	tab timeC if record==1, missing

	collapse (sum) baseline_correct accuracy earningsN timeM (median) session baseline one_token one_tokenPP one_tokenP one_tokenS standard increasing timeC (median) raven_score CRT* `demog', by(sid)

	replace baseline_correct = baseline_correct/100

	generate raven_setI = 100*(raven_score/12)
	generate int nquestions = 36
	generate baseline_pct = 100*(baseline_correct/nquestions)
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate baseline_ratio = baseline_pct/100
	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	* restrict to standard and increasing
*	keep if standard == 1 | increasing == 1
*	bayes, hpd: glm earnings_ratio increasing female black `demog_x', link(p) family(binomial)

	tab female if standard==1
	fracreg probit earnings_ratio female if standard==1, vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio female if standard==1, het(female) vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio female `demog_x' if standard==1, het(female `demog_x') vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio female black `demog_x' if standard==1, het(female black `demog_x') vce(robust)
	margins, dydx(*) post

	tab female black if standard==1
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1 | increasing==1, het(i.female i.black female#black `demog_x') vce(robust)
	margins female#black
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1, het(i.female i.black female#black `demog_x') vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1 | increasing==1, het(i.female i.black female#black `demog_x') vce(robust)
	margins female#black

	tab female black if standard==1 | increasing==1
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' increasing if standard==1 | increasing==1, het(i.female i.black female#black `demog_x' increasing) vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' increasing if standard==1 | increasing==1, het(i.female i.black female#black `demog_x' increasing) vce(robust)
	margins female#black

	tab female black if standard==1 | increasing==1 | timeC==1
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' increasing timeC if standard==1 | increasing==1 | timeC==1, het(i.female i.black female#black `demog_x' increasing timeC) vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' increasing timeC if standard==1 | increasing==1 | timeC==1, het(i.female i.black female#black `demog_x' increasing timeC) vce(robust)
	margins female#black


* econometrics on accuracy and earnings for salient tasks
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if baseline == 0
	keep if standard==1 | one_token==1
	save tmp, replace

	* simple estimates
	collapse (sum) baseline_correct accuracy earningsN timeM (median) raven_score `demog' gpaHI (median) session baseline one_token one_tokenPP one_tokenP one_tokenS standard, by(sid)

	tab standard one_token, missing

	generate raven_setI = 100*(raven_score/12)
	generate int nquestions = 36
	generate baseline_pct = 100*(baseline_correct/nquestions)
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate baseline_ratio = baseline_pct/100
	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format baseline_pct accuracy_pct earnings_pct %8.0f

	summ baseline_ratio

	* total effects
	fracreg probit accuracy_ratio standard 
	margins, dydx(*)
	fracreg probit earnings_ratio standard
	margins, dydx(*)

	* marginal effects, just baseline and one-token
	fracreg probit accuracy_ratio standard `demog'
	margins, dydx(*)

	* marginal effects, just baseline and one-token
	fracreg probit earnings_ratio standard `demog'
	margins, dydx(*)

	* demographics combined
	fracreg probit accuracy_ratio `demog'
	margins, dydx(*)

	fracreg probit earnings_ratio `demog'
	margins, dydx(*)

	* total effects of gender and race just for standard
	fracreg probit accuracy_ratio female if standard==1
	margins, dydx(*)
	fracreg probit accuracy_ratio black if standard==1
	margins, dydx(*)

	fracreg probit earnings_ratio `demog' if standard==1, het(`demog') vce(robust)
	margins, dydx(*) post

	fracreg probit earnings_ratio `demog' if standard==1, vce(robust)
*	betareg earnings_ratio `demog' if standard==1, vce(robust) link(probit) slink(log) scale(female black)
	margins, dydx(*) post

	* save estimates for coefplot
		matrix etable = e(b)
		matrix vtable = e(V)
		matrix list etable
		matrix list vtable

		local c = "standard_e"
		local tt = 0
		foreach t of local demog {
			local tt = `tt'+1
			local p = etable[1,`tt']
			local v = vtable[`tt',`tt']
			local `c'_pe_`t' = 100*`p'
			local `c'_se_`t' = 100*sqrt(`v')
			local `c'_hi_`t' = ``c'_pe_`t'' + invnormal(0.975)*``c'_se_`t''
			local `c'_lo_`t' = ``c'_pe_`t'' + invnormal(0.025)*``c'_se_`t''
		}

	* factor variables
	fracreg probit earnings_ratio i.female i.black `demog_x' if standard==1, vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1, vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1, vce(robust)
	margins female#black

	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1, het(i.female#i.black `demog_x') vce(robust)
	margins, dydx(*) post
	fracreg probit earnings_ratio i.female i.black female#black `demog_x' if standard==1, het(i.female#i.black female#black `demog_x') vce(robust)
	margins female#black

	* total effects of gender and race just for one_token
	fracreg probit accuracy_ratio female if standard==0
	margins, dydx(*)
	fracreg probit accuracy_ratio black if standard==0
	margins, dydx(*)

	* just for each one_token now (coefplot 3)
*	fracreg probit accuracy_ratio female black `demog' if standard==0, het(female black `demog') vce(robust)
	fracreg probit accuracy_ratio female black `demog' if standard==0, vce(robust)
*	betareg accuracy_ratio female black `demog' if standard==0, vce(robust) link(probit) slink(log) scale(female black)
	margins, dydx(*) post

	* save estimates for coefplot
		matrix etable = e(b)
		matrix vtable = e(V)
		matrix list etable
		matrix list vtable

		local c = "one_token_a"
		local tt = 0
		foreach t of local demog {
			local tt = `tt'+1
			local p = etable[1,`tt']
			local v = vtable[`tt',`tt']
			local `c'_pe_`t' = 100*`p'
			local `c'_se_`t' = 100*sqrt(`v')
			local `c'_hi_`t' = ``c'_pe_`t'' + invnormal(0.975)*``c'_se_`t''
			local `c'_lo_`t' = ``c'_pe_`t'' + invnormal(0.025)*``c'_se_`t''
		}

*	fracreg probit earnings_ratio `demog' if standard==0, het(`demog') vce(robust)
	fracreg probit earnings_ratio `demog' if standard==0, vce(robust)
*	betareg earnings_ratio `demog' if standard==0, vce(robust) link(probit) slink(log) scale(female black)
	margins, dydx(*) post

	* total effects
	fracreg probit earnings_ratio female##standard if standard==0 | standard==1, vce(robust)
	margins female##standard, pwcompare(effects)

	fracreg probit earnings_ratio black##standard if standard==0 | standard==1, vce(robust)
	margins black##standard, pwcompare(effects)

	* marginal effects
	fracreg probit accuracy_ratio female##standard age black##standard business##standard noreligion##standard working##standard if standard==0 | standard==1, vce(robust)
	margins female##standard black##standard business##standard noreligion##standard working##standard, pwcompare(effects)

	fracreg probit earnings_ratio female##standard age black##standard business##standard noreligion##standard working##standard if standard==0 | standard==1, vce(robust)
	margins female##standard black##standard business##standard noreligion##standard working##standard, pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "c"
		local tt = 0
		foreach t of local demog {
			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_p_`t'  = 100*rtable[4,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on efficiency
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "c"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel C: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel C: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel C: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel C: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"
	
	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''
	di "Third result of the paper, on demographics: panel C: effect on `t' is ``c'_pe_`t'' with p-value ``c'_p_`t''"

	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -30(10)70, suffix(%) myscale(@) local(pct)
	mylabels -40(10)60, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') title("{bf:C. Efficiency:} Eighty Tokens Progressive {it:versus} One Token Progressive", size(medlarge)) msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) saving(`c'_effects_, replace)

	* combined
	gr combine a_effects_.gph b_effects_.gph c_effects_.gph, rows(3) imargin(vsmall) title("Figure `appendix'7: Effects of Demographics with" "Progressive Raven Problems", size(vlarge)) subtitle("All results used the original progressive order" "Average marginal effects from Fractional Regression" "`extra_text'", size(small) margin(small)) saving(raven_`appendix'7, replace)
	gr export raven_7.png, replace

	* clean up
	foreach x in baseline one_token standard {
	foreach y in a e {
		capture: erase `x'_`y'_effects_.gph
	}
	}
	erase a_effects_.gph
	erase b_effects_.gph
	erase c_effects_.gph



* figures 5 and 6
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if baseline == 0
	keep if standard==1 | one_token==1
	drop if scrambled==1

	collapse (mean) baseline_correct accuracy earningsN, by(one_token question)

	replace accuracy = 100*accuracy
	replace earningsN = 100*earningsN
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)

	local b0 = "black"
	local b1 = "gs9"
	local b2 = "gs12"
	twoway (connected baseline_correct question, sort mcolor(`b0') msymbol(point) lcolor(`b0') lwidth(thick)) (connected accuracy question if one_token==1, sort mcolor(`b1') msymbol(point) lcolor(`b1') lwidth(thick)) (connected accuracy question if one_token==0, sort mcolor(`b2') msymbol(point) lcolor(`b2') lwidth(thick)), ytitle("Accuracy") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix'5: Average Accuracy and Incentives" "with Progressive Raven Problems", size(vlarge)) subtitle("Accuracy measured by percent of tokens allocated to the correct answer" "`extra_text'", size(small) margin(small)) legend(order(1 "Baseline (N=`n_baseline')" 2 "Accuracy with One Token (N=`n_one_token')" 3 "Accuracy with 80 Tokens (N=`n_standard')") rows(3) position(7) ring(0) size(small)) saving(figures/`appendix'figure5_bw, replace)
	twoway (connected baseline_correct question, sort mcolor(black) msymbol(point) lcolor(black) lwidth(thick)) (connected accuracy question if one_token==1, sort mcolor(red) msymbol(point) lcolor(red) lwidth(thick)) (connected accuracy question if one_token==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)), ytitle("Accuracy") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix'5: Average Accuracy and Incentives" "with Progressive Raven Problems", size(vlarge)) subtitle("Accuracy measured by percent of tokens allocated to the correct answer" "`extra_text'", size(small) margin(small)) legend(order(1 "Baseline (N=`n_baseline')" 2 "Accuracy with One Token (N=`n_one_token')" 3 "Accuracy with 80 Tokens (N=`n_standard')") rows(3) position(7) ring(0) size(small)) saving(raven_`appendix'5, replace)
	gr export raven_5.png, replace

	twoway (connected earningsN question if one_token==1, sort mcolor(`b1') msymbol(point) lcolor(`b1') lwidth(thick)) (connected earningsN question if one_token==0, sort mcolor(`b2') msymbol(point) lcolor(`b2') lwidth(thick)), ytitle("Efficiency") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix'6: Average Efficiency with" "Progressive Raven Problems", size(vlarge)) subtitle("Efficiency measured by percent of realized earnings compared to maximum earnings" "Uniform token allocation ensures earning $1.13 and efficiency of 56.5%" "`extra_text'", margin(small) size(small)) legend(order(1 "Efficiency with One Token (N=`n_one_token')" 2 "Efficiency with 80 Tokens (N=`n_standard')") rows(2) position(7) ring(0) size(small)) yline(56.5, lcolor(black) lpattern(dash)) saving(figures/`appendix'figure6_bw, replace)
	twoway (connected earningsN question if one_token==1, sort mcolor(red) msymbol(point) lcolor(red) lwidth(thick)) (connected earningsN question if one_token==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)), ytitle("Efficiency") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix'6: Average Efficiency with" "Progressive Raven Problems", size(vlarge)) subtitle("Efficiency measured by percent of realized earnings compared to maximum earnings" "Uniform token allocation ensures earning $1.13 and efficiency of 56.5%" "`extra_text'", margin(small) size(small)) legend(order(1 "Efficiency with One Token (N=`n_one_token')" 2 "Efficiency with 80 Tokens (N=`n_standard')") rows(2) position(7) ring(0) size(small)) yline(56.5, lcolor(black) lpattern(dash)) saving(raven_`appendix'6, replace)
	gr export raven_6.png, replace


* display the pattern of choices
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear
	keep if baseline == 0
	keep if standard == 1
	drop if scrambled==1
	keep session sid r1-r8 question
	drop if sid==. | question==.
	reshape long r, i(sid question) j(bin)

	* display over all questions, pooled over subjects
	graph bar (mean) r, over(bin) bar(1, fcolor(blue)) ytitle("") by(, title("Figure 8: Allocation of Tokens by Raven Question", size(vlarge) span) subtitle("Pooled over incentivized responses in the Eighty Tokens Progressive condition", size(small)) note("")) by(question, noyrescale noxrescale noiyaxes noiytick noiylabel noiytitle) saving(raven_8, replace)
	gr export raven_8.png, replace

	* display specific questions
	egen int sid_new = group(sid)
	if "$doGraphs" == "y" {
		forvalues x=1/36 {
			graph bar (asis) r if question==`x', over(bin) bar(1, fcolor(blue)) ytitle("") by(, title("Allocation of Tokens in Raven Question #`x'", size(vlarge) span) subtitle("Incentivized responses in the Eighty Tokens Progressive condition. The correct answer is `c`x''", size(small)) note("")) by(sid_new, noyrescale noxrescale noiyaxes noiytick noiylabel noiytitle) saving(figures/progressive_q_`x', replace)
		}
	}
	else {
		local x = 20
		graph bar (asis) r if question==`x' & sid_new<=20, over(bin) bar(1, fcolor(blue)) ytitle("") by(, title("Figure 9: Allocation of Tokens in Raven Question #`x'", size(vlarge) span) subtitle("Incentivized responses in the Eighty Tokens Progressive condition. The correct answer is `c`x''", size(small)) note("")) by(sid_new, noyrescale noxrescale noiyaxes noiytick noiylabel noiytitle) saving(raven_9, replace)
		gr export raven_9.png, replace
	}

* get the collated data
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	* get token allocations
	forvalues x=1/8 {
		generate int ntokens`x' = r`x'*tokens
	}
	des ntokens*

	* flag those with all tokens in one bin
	generate int nbins = 0
	generate int uniform = 1
	forvalues x=1/8 {
		replace nbins = nbins+1 if ntokens`x'>0
		replace uniform = 0 if ntokens`x' ~= 10
	}
	generate int type1 = 0
	replace type1 = 1 if nbins == 1
	label variable type1 "Allocation all tokens to just one answer"
	generate int type3 = 0
	replace type3 = 1 if uniform == 1
	label variable type3 "Allocation all same tokens to all answers"

	* residual type 2
	generate int type2 = 0
	replace type2 = 1 if type1 == 0 & type3 == 0
	label variable type2 "Allocations between certainty and uniform"

	sort question
	list r1-r8 type1 type2 type3 if question == 20 & incentives==1, noobs
	tab question type1 if incentives==1
	tab question type2 if incentives==1
	tab question type3 if incentives==1

	generate int type = .
	replace type = 1 if type1==1
	replace type = 2 if type2==1
	replace type = 3 if type3==1
	label variable type "Type of decision maker"
	tab type if question==1, missing
	tab type if question==20, missing
	tab type if question==36, missing

	generate type_sum = type1 + type2 + type3
	generate type1_share = 100*(type1/type_sum)
	generate type2_share = 100*(type2/type_sum)
	generate type3_share = 100*(type3/type_sum)

	mylabels 0(25)100, suffix(%) myscale(@) local(pct)
	local fig = 10
	twoway (lpoly type1_share question, lcolor(blue) lwidth(thick)) (lpoly type2_share question, lcolor(blue) lwidth(thick) lpattern(shortdash_dot)) (lpoly type3_share question, lcolor(blue) lwidth(thick) lpattern(dash)) if standard==1, ytitle(Percent) ytitle(, size(medlarge) orientation(vertical) margin(vsmall)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medlarge) margin(small)) xlabel(0(12)36) title("A. Type of Response", size(large)) subtitle("{bf:Certain} allocate all tokens to one possible solution" "{bf:Diffuse} allocate all tokens equally to all possible solutions" "{bf:Unsure} is any allocation that is not {bf:Certain} or {bf:Diffuse}", size(small)) legend(order(1 "Certain" 2 "Unsure" 3 "Diffuse") size(small) rows(3) position(1) ring(0)) saving(type, replace)
	twoway (lpoly type1_share question) (lpoly type2_share question) (lpoly type3_share question) if incentives == 1 & tokens == 80 & increasing == 1, ytitle(Percent) ytitle(, size(medlarge) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question) xtitle(, size(medlarge) margin(medium)) xlabel(0(12)36) title(Time Trend of Type of Response, size(vlarge)) subtitle("Responses to incentivized QSR with 80 tokens" "Assuming increasing incentives" "90-minute time constraint") legend(order(1 "Certain" 2 "Unsure" 3 "Diffuse") rows(3) position(1) ring(0)) saving(figures/`appendix'type_shares_increasing, replace)

	* time taken
	local fig = 11
	twoway (lpoly timeS question if one_token==1, lcolor(red) lwidth(thick)) (lpoly timeS question if standard==1, lcolor(blue) lwidth(thick)), ytitle("Seconds") ytitle(, size(medlarge) orientation(vertical) margin(vsmall)) ylabel( , angle(horizontal)) xtitle(Question) xtitle(, size(medlarge) margin(small)) xlabel(0(12)36) title("B. Time Taken", size(large)) subtitle("Smoothed trend lines", size(small) margin(medium)) legend(order(1 "One Token" 2 "Eighty Tokens") size(small) rows(2) position(11) ring(0)) saving(time, replace)

	* combine type and time
	gr combine type.gph time.gph, cols(2) imargin(small) title("Figure `appendix'10: Time Trends of Time for Response" "and for Type of Response", size(vlarge)) saving(raven_`appendix'10, replace)
	gr export raven_10.png, replace
	erase time.gph
	erase type.gph

	* effect of scrambled -- 80 tokens
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if baseline == 0
	keep if standard==1 | scrambled==1

	drop if one_token == 1
	drop if increasing == 1
	keep if time == 90

	collapse (mean) baseline_correct accuracy earningsN, by(scrambled question)

	replace accuracy = 100*accuracy
	replace earningsN = 100*earningsN
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)

	local b0 = "black"
	local b1 = "gs9"
	local b2 = "gs12"
	twoway (connected accuracy question if scrambled==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)) (connected accuracy question if scrambled==1, sort mcolor(green*0.7) msymbol(point) lcolor(green*0.7) lwidth(thick)), ytitle("Accuracy") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix': Average Accuracy" "with Scrambled Raven Problems", size(vlarge)) subtitle("Accuracy measured by percent of 80 tokens allocated to the correct answer" "`extra_text'" "Question number from original progressive order", size(small) margin(small)) legend(order(1 "Progressive Accuracy (N=`n_progressive80')" 2 "Scrambled Accuracy (N=`n_scrambled80')") rows(3) position(7) ring(0) size(small)) saving(figures/scrambled_accuracy, replace)
	gr export scrambled_accuracy.png, replace

	twoway (connected earningsN question if scrambled==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)) (connected earningsN question if scrambled==1, sort mcolor(green*0.7) msymbol(point) lcolor(green*0.7) lwidth(thick)), ytitle("Efficiency") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix': Average Efficiency" "with Scrambled Raven Problems", size(vlarge)) subtitle("Efficiency measured by percent of realized earnings compared to maximum earnings" "Uniform 80 token allocation ensures earning $1.13 and efficiency of 56.5%" "`extra_text'" "Question number from original progressive order", margin(small) size(small)) legend(order(1 "Progressive Efficiency (N=`n_progressive80')" 2 "Scrambled Efficiency (N=`n_scrambled80')") rows(2) position(7) ring(0) size(small)) yline(56.5, lcolor(black) lpattern(dash)) saving(figures/scrambled_efficiency, replace)
	gr export scrambled_efficiency.png, replace


	* effect of scrambled -- 80 tokens
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if baseline == 0
	keep if standard==1 | scrambled==1

	drop if one_token == 1
	drop if increasing == 1
	keep if time == 90

	collapse (sum) baseline_correct accuracy earningsN (median) raven_score `demog' gpaHI scrambled, by(sid)

	generate int nquestions = 36
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format accuracy_pct earnings_pct %8.0f


	* marginal effects
	fracreg probit accuracy_ratio female##scrambled age black##scrambled business##scrambled noreligion##scrambled working##scrambled if scrambled==0 | scrambled==1, vce(robust)
	margins female##scrambled black##scrambled business##scrambled noreligion##scrambled working##scrambled, pwcompare(effects)

	fracreg probit earnings_ratio female##scrambled age black##scrambled business##scrambled noreligion##scrambled working##scrambled if scrambled==0 | scrambled==1, vce(robust)
	margins female##scrambled black##scrambled business##scrambled noreligion##scrambled working##scrambled, pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "c"
		local tt = 0
		foreach t of local demog {
			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on efficiency
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "c"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''
	
	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''

	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -30(10)30, suffix(%) myscale(@) local(pct)
	local app = "`appendix'"
	local appendix = "D"
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) title("Figure `appendix'7: Effects of Scrambling on Efficiency", size(vlarge)) subtitle("All results used 80 tokens" "Average marginal effects of Scrambling from Fractional Regression" "`extra_text'", size(small) margin(small)) saving(figures/`appendix'figure7_s, replace)
	gr export raven_app1.png, replace
	local appendix = "`app'"

* effect of one-token in scrambled
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if scrambled==1 
	drop if increasing == 1
	keep if time == 90

	collapse (mean) baseline_correct accuracy earningsN, by(one_token question)

	replace accuracy = 100*accuracy
	replace earningsN = 100*earningsN
	mylabels 0(25)100, suffix(%) myscale(@) local(pct)

	local b0 = "black"
	local b1 = "gs9"
	local b2 = "gs12"
	twoway (connected accuracy question if one_token==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)) (connected accuracy question if one_token==1, sort mcolor(green*0.7) msymbol(point) lcolor(green*0.7) lwidth(thick)), ytitle("Accuracy") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix': Average Accuracy" "with Scrambled Raven Problems", size(vlarge)) subtitle("Accuracy measured by percent of tokens allocated to the correct answer" "`extra_text'" "Question number from original progressive order", size(small) margin(small)) legend(order(1 "Eighty Tokens Accuracy (N=`n_scrambled80')" 2 "One Token Accuracy (N=`n_scrambled1')") rows(3) position(7) ring(0) size(small)) saving(scrambled_accuracy_1_80, replace)
	gr export scrambled_accuracy_1_80.png, replace

	twoway (connected earningsN question if one_token==0, sort mcolor(blue) msymbol(point) lcolor(blue) lwidth(thick)) (connected earningsN question if one_token==1, sort mcolor(green*0.7) msymbol(point) lcolor(green*0.7) lwidth(thick)), ytitle("Efficiency") ytitle(, size(medium) orientation(horizontal) margin(medium)) ylabel(`pct', angle(horizontal)) xtitle(Question Number) xtitle(, size(medium)) xlabel(0(12)36) title("Figure `appendix'11: Average Efficiency" "with Scrambled Raven Problems", size(vlarge)) subtitle("Efficiency measured by percent of realized earnings compared to maximum earnings" "Uniform token allocation ensures earning $1.13 and efficiency of 56.5%" "`extra_text'" "Question number from original progressive order", margin(small) size(small)) legend(order(1 "Eighty Tokens Efficiency (N=`n_scrambled80')" 2 "One Token Efficiency (N=`n_scrambled1')") rows(2) position(7) ring(0) size(small)) yline(56.5, lcolor(black) lpattern(dash)) saving(raven_`appendix'11, replace)
	gr export raven_11.png, replace


* effect of one_token in scrambled -- basic demographics
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if scrambled==1 
	drop if increasing == 1
	keep if time == 90

	collapse (sum) baseline_correct accuracy earningsN (median) raven_score `demog' gpaHI one_token, by(sid)

	generate int nquestions = 36
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format accuracy_pct earnings_pct %8.0f

	* get effect of 80 tokens
	generate int eighty_tokens = 0
	replace eighty_tokens = 1 if one_token == 0
	label variable eighty_tokens "Subject had 80 tokens to allocate"

	* marginal effects -- no interaction with working
	fracreg probit accuracy_ratio female##eighty_tokens age black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens if eighty_tokens==0 | eighty_tokens==1, vce(robust)
	margins female##eighty_tokens black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens, pwcompare(effects)

	fracreg probit earnings_ratio female##eighty_tokens age black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens if eighty_tokens==0 | eighty_tokens==1, vce(robust)
	margins female##eighty_tokens black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens, pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "c"
		local tt = 0
		foreach t of local demog {
			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on efficiency
	matrix e = J(5,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working"

	local c = "c"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''

	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''
	
	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"

	mylabels -10(10)40, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) title("Figure `appendix'12: Effects of Eighty Tokens on Efficiency" "with Scrambled Raven Problems", size(vlarge)) subtitle("Average marginal effects of 80 Tokens from Fractional Regression" "Pooling 80 token and 1 token results" "Solely comparing results within the Scrambled Treatment" "`extra_text'", size(small) margin(small)) saving(raven_`appendix'12_old, replace)
	gr export raven_12_old.png, replace


* effect of one_token in scrambled -- basic demographics + BFI
	di "Data from GSU_raven_main`file'..."
	use GSU_raven_main`file', clear

	keep if scrambled==1 
	drop if increasing == 1
	keep if time == 90

	collapse (sum) baseline_correct accuracy earningsN (median) raven_score `demog' bfi_* one_token, by(sid)

	generate int nquestions = 36
	generate accuracy_pct = 100*(accuracy/nquestions)
	generate earnings_pct = 100*(earningsN/nquestions)

	generate accuracy_ratio = accuracy_pct/100
	generate earnings_ratio = earnings_pct/100

	format accuracy_pct earnings_pct %8.0f

	* get effect of 80 tokens
	generate int eighty_tokens = 0
	replace eighty_tokens = 1 if one_token == 0
	label variable eighty_tokens "Subject had 80 tokens to allocate"

	* marginal effects -- no interaction with working
	fracreg probit accuracy_ratio female##eighty_tokens age black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens if eighty_tokens==0 | eighty_tokens==1, vce(robust)
	margins female##eighty_tokens black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens, pwcompare(effects)

	fracreg probit earnings_ratio female##eighty_tokens age black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens if eighty_tokens==0 | eighty_tokens==1, vce(robust)
	margins female##eighty_tokens black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens, pwcompare(effects)

	local bfi "bfi_extra bfi_agree bfi_cons bfi_neur bfi_open"

	* add in bfi, converting to binary
	replace bfi_extra = 0 if bfi_extra<=2
	replace bfi_extra = 1 if bfi_extra>=3
	replace bfi_agree = 0 if bfi_agree<=3
	replace bfi_agree = 1 if bfi_agree>=4
	replace bfi_cons = 0 if bfi_cons<=2
	replace bfi_cons = 1 if bfi_cons>=3
	replace bfi_neur = 0 if bfi_neur<=2
	replace bfi_neur = 1 if bfi_neur>=3
	replace bfi_open = 0 if bfi_open<=3
	replace bfi_open = 1 if bfi_open>=4
	summ bfi*


	fracreg probit earnings_ratio female##eighty_tokens age black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens bfi_extra##eighty_tokens bfi_agree##eighty_tokens bfi_cons##eighty_tokens bfi_neur##eighty_tokens bfi_open##eighty_tokens if eighty_tokens==0 | eighty_tokens==1, vce(robust)
	margins female##eighty_tokens black##eighty_tokens business##eighty_tokens noreligion##eighty_tokens working##eighty_tokens bfi_extra##eighty_tokens bfi_agree##eighty_tokens bfi_cons##eighty_tokens bfi_neur##eighty_tokens bfi_open##eighty_tokens , pwcompare(effects)

	* save estimates for coefplot

		matrix rtable = r(table_vs)
		matrix list rtable

		local c = "c"
		local tt = 0

		local rhs "female age black business noreligion working bfi_extra bfi_agree bfi_cons bfi_neur bfi_open"
		foreach t of local rhs {

			if "`t'" == "female" {
				local tt = 8
			}
			if "`t'" == "black" {
				local tt = 15
			}
			if "`t'" == "business" {
				local tt = 22
			}
			if "`t'" == "noreligion" {
				local tt = 29
			}
			if "`t'" == "working" {
				local tt = 36
			}
			if "`t'" == "bfi_extra" {
				local tt = 43
			}
			if "`t'" == "bfi_agree" {
				local tt = 50
			}
			if "`t'" == "bfi_cons" {
				local tt = 57
			}
			if "`t'" == "bfi_neur" {
				local tt = 64
			}
			if "`t'" == "bfi_open" {
				local tt = 71
			}

			local `c'_pe_`t' = 100*rtable[1,`tt']
			local `c'_hi_`t' = 100*rtable[6,`tt']
			local `c'_lo_`t' = 100*rtable[5,`tt']
		}


	* now the pictures showing effects on efficiency
	matrix e = J(10,3,.)
	matrix coln e = pe low high
	matrix rown e = "female" "black" "business" "noreligion" "working" "bfi_extra" "bfi_agree" "bfi_cons" "bfi_neur" "bfi_open"

	local c = "c"

	local t = "female"

	matrix e[1,1] = ``c'_pe_`t''
	matrix e[1,2] = ``c'_lo_`t''
	matrix e[1,3] = ``c'_hi_`t''

	local t = "black"

	matrix e[2,1] = ``c'_pe_`t''
	matrix e[2,2] = ``c'_lo_`t''
	matrix e[2,3] = ``c'_hi_`t''

	local t = "business"

	matrix e[3,1] = ``c'_pe_`t''
	matrix e[3,2] = ``c'_lo_`t''
	matrix e[3,3] = ``c'_hi_`t''
	
	local t = "noreligion"

	matrix e[4,1] = ``c'_pe_`t''
	matrix e[4,2] = ``c'_lo_`t''
	matrix e[4,3] = ``c'_hi_`t''

	local t = "working"

	matrix e[5,1] = ``c'_pe_`t''
	matrix e[5,2] = ``c'_lo_`t''
	matrix e[5,3] = ``c'_hi_`t''

	local t = "bfi_extra"

	matrix e[6,1] = ``c'_pe_`t''
	matrix e[6,2] = ``c'_lo_`t''
	matrix e[6,3] = ``c'_hi_`t''

	local t = "bfi_agree"

	matrix e[7,1] = ``c'_pe_`t''
	matrix e[7,2] = ``c'_lo_`t''
	matrix e[7,3] = ``c'_hi_`t''
	
	local t = "bfi_cons"

	matrix e[8,1] = ``c'_pe_`t''
	matrix e[8,2] = ``c'_lo_`t''
	matrix e[8,3] = ``c'_hi_`t''

	local t = "bfi_neur"

	matrix e[9,1] = ``c'_pe_`t''
	matrix e[9,2] = ``c'_lo_`t''
	matrix e[9,3] = ``c'_hi_`t''

	local t = "bfi_open"

	matrix e[10,1] = ``c'_pe_`t''
	matrix e[10,2] = ``c'_lo_`t''
	matrix e[10,3] = ``c'_hi_`t''

	matrix list e

	mylabels -20(10)45, suffix(%) myscale(@) local(pct)
	label variable female "Female"
	label variable black "Black"
	label variable business "Business Major"
	label variable noreligion "Atheist"
	label variable working "Working"
	label variable bfi_extra "BFI Extraversion"
	label variable bfi_agree "BFI Agreeableness"
	label variable bfi_cons "BFI Conscientiousness"
	label variable bfi_neur "BFI Neuroticism"
	label variable bfi_open "BFI Openness"

	mylabels -10(10)40, suffix(%) myscale(@) local(pct)
	coefplot matrix(e[.,1]), ci((e[.,2] e[.,3])) ciopts(lcolor(blue) recast(rcap)) xline(0, lpattern(dash) lcolor(red)) grid(none) scheme(s1color) ytitle("") xtitle("") xlabel(`pct') msymbol(D) mcolor(blue) ylabel(, labsize(medsmall)) yscale(alt) title("Figure `appendix'12: Effects of Eighty Tokens on Efficiency" "with Scrambled Raven Problems", size(vlarge) span) subtitle("Average marginal effects of 80 Tokens from Fractional Regression" "Pooling 80 token and 1 token results" "Solely comparing results within the Scrambled Treatment" "`extra_text'", size(small) margin(small) span) saving(raven_`appendix'12, replace)
	gr export raven_12.png, replace

* PDF document
* start a PDF
	putpdf begin, font("Candara",12) margin(left,1) margin(right,1)

	putpdf paragraph, font("",14) halign(center)
	putpdf text ("Analysis of Raven Data from 2019 and 2022"), bold linebreak(2)

	local d = c(current_date)
	local t = c(current_time)
	putpdf paragraph, font("",12) halign(center)
	putpdf text ("`d'   `t'"), linebreak(2)

	putpdf paragraph, font("",12) halign(left)

	putpdf text ("        To date there have been `Nsub' subjects, spanning sessions in 2019 and 2022, each reporting their beliefs on the 8 alternatives for each of 36 of the Raven Progressive Matrices test of ")
	putpdf text ("fluid intelligence. The belief interface employs a Quadratic Scoring Rule (QSR) ")
	putpdf text ("with parameters chosen to allow a perfect allocation of 80 tokens to the correct interval to be rewarded with $2. ")
	putpdf text ("Subjects must allocate all 80 tokens for 36 different tasks, with each task paid. ")
	putpdf text (" "), linebreak(2)

	putpdf text ("        One of the new treatments, imaginatively called ")
	putpdf text ("Scrambled"), bold
	putpdf text (", is to scramble the order of presenting the matrices to the subjects. The goal here is to update figures from the original study, ")
	putpdf text ("looking at the effects of this treatent as well as the increased sample sizes. The figure numbering follows the original study. ")
	putpdf text (" "), linebreak(2)

	putpdf text ("        Samples for the core treatments, spanning the 2019 and 2022 sessions, are as follows: "), linebreak(1)
	putpdf text ("Baseline with 80 Tokens"), bold
	putpdf text (" = `n_progressive80', ")
	putpdf text ("Baseline with 1 Token"), bold
	putpdf text (" = `n_progressive1', ")
	putpdf text ("Scrambled with 80 Tokens"), bold
	putpdf text (" = `n_scrambled80', and ")
	putpdf text ("Scrambled with 1 Token"), bold
	putpdf text (" = `n_scrambled1'. ")
	local Ncore = `n_progressive80' + `n_progressive1' + `n_scrambled80' + `n_scrambled1'
	local Nother = `Nsub' - `n_progressive80' - `n_progressive1' - `n_scrambled80' - `n_scrambled1'
	putpdf text ("Hence there are `Ncore' subjects in the core treatments. This means that `Nother' = `Nsub' - `Ncore' subjects were in other treatments, such as a time limit, increasing incentives with problem difficulty, or the use of short-form batteries. ")
	putpdf text (" "), linebreak(2)
	
	putpdf text ("        The analyses here refer only to analyses of reports of beliefs. There has been no attempt so far to recover beliefs based on inferred risk preferences. ")
	putpdf text (" "), linebreak(2)

	* reports
	local page = 1

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_5.png, linebreak width(6)
	putpdf image raven_6.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_7.png, linebreak width(6)
	putpdf image raven_10.png, linebreak width(6)

	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_8.png, linebreak width(6)
	putpdf image raven_9.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image scrambled_accuracy.png, linebreak width(6)
	putpdf image scrambled_efficiency.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_app1.png, linebreak width(6)
	putpdf image scrambled_accuracy_1_80.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_11.png, linebreak width(6)
	putpdf image raven_12.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_B1.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_E1.png, linebreak width(6)
	putpdf image raven_E2.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_E3.png, linebreak width(6)

	putpdf pagebreak
	putpdf paragraph, halign(right) font("Candara",10)
	local page = `page' + 1
	putpdf text ("Page `page'"), linebreak(1)
	putpdf paragraph, halign(center)
	putpdf image raven_E4.png, linebreak width(6)
	putpdf image raven_E5.png, linebreak width(6)

* save the PDF report
	putpdf save "Analysis of Raven Data from 2019 and 2022.pdf", replace

* clean up
foreach x in 5 6 7 8 9 10 11 12 _app1 _E1 _E2 _E3 _E4 _E5 _E6 _E7 _B1 {
	capture: erase raven_`x'.png
}
foreach x in scrambled_accuracy scrambled_efficiency scrambled_accuracy_1_80 {
	capture: erase `x'.png
}

* clean up
capture: erase tmp.dta
capture: erase tmp_risklink.dta

* time taken
timer off 1
timer list
local secs = r(t1)
local mins = `secs'/60
local hrs = `mins'/60
local secs_ = string(`secs', "%10.0f")
local mins_ = string(`mins', "%4.1f")
local hrs_ = string(`hrs', "%4.2f")
di "Calculations took `secs_' seconds, `mins_' minutes, or `hrs_' hours."

log close raven

